Impossible: Property using Generics in Delphi. Interfaces/classes/methods/fields/constraints/type inferencing are
Posted by jpluimers on 2013/01/30
Just in case you wonder about Property using Generics in Delphi, they are not possible.
Thanks David for mentioning it, Hallvard for mentioning it even earlier and Rudy for confirming it.
These are supported with Generics in Delphi:
- Generic interfaces
- Generic classes
- Generic methods
- Generic fields
- Generic type inferencing
- Generic constraints (a bit buggy in Delphi 2009)
All of the supported aspects are linked to articles from excellent authors. There is far more on the internet about Delphi and Generics, but those are a good start.
Thanks Malcolm, Phil, Barry, Hallvard, Jolyon and many others for posting all those articles!
Note that this is not possible in C# either, Julian Bucknall organized a chat and explains why, but there is a workaround which I might try to port to Delphi in the future.
–jeroen






hvassbotn said
Hi Jeroen,
Thanks for the mention :).
I did point this out in my old Delphi for .NET article, actually :)
“Although, one might think that having support for generic properties as well would be a natural extension (after all a property just maps to getter and setter methods, after all), but indeed this is not possible. I don’t think C# supports this, either, btw.
type
// Generic property – not supported
TBar = class
procedure SetGenericProperty(const A: T);
function GetGenericProperty: T;
// NOTE: Is this not allowed – Would be cool?!
property GenericProperty: T read GetGenericProperty write SetGenericProperty;
end;”
jpluimers said
LOL, you are welcome. And so much for my reading skills (:
jpluimers said
I did some more research, and modified the post:
– attribution to you for mentioning it even earlier
– indicating that in C# this is indeed impossible