I know that in JAVA there is the possibility of declaring a generic with a mask List<? extends Number> List<? extends Number> . Is there a possibility of such an implementation on DELPHI?
1 answer
Such an approach in Delphi is called constraints .
As a limitation, you can specify that the type of the T parameter:
- Supports the specified
TMyList<T: IMyInteface> - Is a derived (inherited) class from the specified
TControlList<T:TWinControl> - Has a default constructor (public, no parameters)
TTest<T:constructor> - Is a class
TMyObjectList<T:class> - is a
valuetype, i.e. structure, number, etc.TValueList<T:record>
You can also specify several constraints in a row by combining some of them (it’s obviously impossible to write class, record at the same time). For example, TTest<T: IMyInterface1, IMyInterface2> (in this case both conditions must be met).
|
? extends Number? extends Number- allows to create only a numeric list as a result. In Delphi did not meet, but in general, you can create your own class of the form:TMyList<T: TBar> = class(TList<T>). - androschuk