Is it possible to accept *any* generic as a parameter? lazarus / 62-General ---------------------------------------------------------------------- hayanninja · 2018-01-01 01:39 Let's say I have a generic descendant of TFPGObjectList<>: Code: [Select]TMoreSpecificObjectList = class(specialize TFPGObjectList) The intent being not to use this with TMoreSpecificObject itself of course, but with descendants of such. Now, I have a procedure: Code: [Select]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: Code: [Select]procedure .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)", 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.) ---------------------------------------------------------------------- ASerge · 2018-01-01 12:00 Generics are used as a template for code generation (for lazy ones). No inheritance and all OOP features. Simple duplication of code. You can make a procedure template and the compiler generates as many procedures as there are types. But better (do not be lazy) refuse generics and use standard class with inheritance and type checking. ---------------------------------------------------------------------- PascalDragon · 2018-01-02 21:35 As long as your is not a generic itself you can do this in trunk/3.1.1 (allowing generic declarations inside generics is a ToDo item): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  generic TMoreSpecificObjectList = class(specialize TFPGObjectList)  end;   TWhatEver = class    generic procedure SetListBoxFromList(aSrc: specialize TMoreSpecificObjectList; aDst: TListBox);  end; generic procedure TWhatEver.SetListBoxFromList(aSrc: specialize TMoreSpecificObjectList; aDst: TListBox);var  OldIndex: Integer;begin  OldIndex := aDst.ItemIndex;  aSrc.WriteNamesToStrings(aDst.Items);  aDst.ItemIndex := OldIndex;end; begin  whateverinstance.specialize SetListBoxFromList(MySubTypeListVar, TargetList);end.  Please note that due to a bug you currently can't use the constraint for the TMoreSpecificObjectList<> as then the implementation of SetListBoxFromList<>() will complain (just test it, you'll see what I mean).