I'm not sure if this is possible in Delphi. I looked around and could not find the answer (example or invalidity that this is impossible):
I have a generic list class and I want to create an instance of type generic. For instance:
type TTypeA = class(TObject); procedure Test; var MyList: TobjectList<TTypeA>; NewListObject: TTypeA; begin MyList := TObjectList<TTypeA>.Create; NewListObject := MyList.xxx
Is it possible to create a function xxx that creates a new object of type TTypeA?
@jeroen: thanks for the answer below. However, I forgot about the important detail in my question:
I would like this code to work for any other type, so without prior knowledge of type T for TObjectList. I could create the following lists:
MyList: TObjectList<TCar>; MyList: TObjectList<TBike>;
Not knowing if MyList contains TCar or TBike (both derived from the same base class and equal constructors), I want to add a new element to MyList.
And with a proposal from Uwe Raabe, I ran into the following problem:
I changed my class to
TMyObjectList<T:class, constructor> = class(TMyBaseObjectList<T>)
where TMyBaseObjectList is defined as
TMyBaseObjectList<T:TMyBaseObject> = class(TObjectList)
Now I get an error: A parameter of type "T" is incompatible with type "T: TMyBaseObject"
source share