Let's say I have a generic descendant of TFPGObjectList<>:
TMoreSpecificObjectList<T: TMoreSpecificObject> = class(specialize TFPGObjectList<T>)
The intent being not to use this with TMoreSpecificObject itself of course, but with descendants of such.
Now, I have a procedure:
procedure TMoreSpecificObjectList.WriteNamesToStrings(aDst: TStrings);
var
Item: TMoreSpecificObject;
begin
aDst.Clear;
for Item in Self do
aDst.Add(Item.Name);
end;
(Of course, the "Name" property exists on TMoreSpecificObject, not only some / all of its descendants.)
Finally, I want to do this somewhere else:
procedure <whatever>.SetListBoxFromList(aSrc: TMoreSpecificObjectList; aDst: TListBox);
var
OldIndex: Integer;
begin
OldIndex := aDst.ItemIndex;
aSrc.WriteNamesToStrings(aDst.Items);
aDst.ItemIndex := OldIndex;
end;
(Perhaps because I want to be able to call "SetListBoxFromList" for several TMoreSpecificObjectList specializations, each of which specialize to a different TMoreSpecificObject descendant.)
In particular, note that I don't need any specific features of specific TMoreSpecificObject descendants; just the "Name" property that they all have (since it's in TMoreSpecificObject or an ancestor of such).
The above code does not work, saying that an unspecialized generic cannot be used as a variable type. Is there any way around this, short of creating a type that's specifically "TMoreSpecificObjectList = class(specialize TFPGObjectList<TMoreSpecificObject>)", implementing this functionality in that, and then creating another generic class that descends from that?
(Note: All code samples in this post were quickly mocked up to demonstrate what I'm trying to do. I'm well aware that the code snippets here are leaving out important checks and stuff.)