Is possible to create an generic constrained method?
-
27-10-2019 - |
Question
I created the following simple class in my code and I get "TObject is not a valid constraint" error message.
TTest = class
class function Test<T:TObject>(Dummy: integer): string;
end;
...
class function TTeste.Teste<T>(Dummy: integer): string;
begin
Result := 'Compiled';
end;
What I'm doing wrong? Is this possible? (You can include option available on later versions than D2010, since we'll receive our XE2 licenses in the next 2 weeks).
Solution
Try the class
constraint:
TTest = class
class function Test<T:class>(Dummy: integer): string;
end;
It might not only accept normal classes, but also interfaces with that constraint(in C# it does, and Delphi generics imitate C# generics).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow