Property using Generics in Delphi
delphi, generics, properties
Solution
Generic properties are not supported in Delphi. Only generic classes, or generic methods.
I can't find anything in the documentation that explicitly states that limitation. On the other hand the documentation only describes generic classes and generic methods. And the new language grammar to support generics also makes no mention of properties.
Problem
I'm trying to write a property which uses generics: ``` type TMyClass = class protected function GetCountBy<T: Class>: Integer; public property CountBy<T: Class>: Integer read GetCountBy<T>; end; ``` but the compile fails on the property declaration with the message 'Property CountBy does not exist in base class', and the red squiggle on the opening < of the property name. Is there any way to achieve this? Edit: Here's my other use case, which is more complex but more real world: ``` property ItemsBy<T: Class>[Index: Integer]: T read GetItemsBy<T> write SetItemsBy<T>; ``` The function filters the contents of a list to return the Index'th item of the specified class.