[fpc-devel] Additional generic type constraints

Kostas Michalopoulos badsectoracula at gmail.com
Wed Feb 22 10:36:59 CET 2023


> Because Delphi doesn't have them and when constraints were implemented 
> they were implemented for Delphi compatibility.

Can they be added? The original announcement ~13 years ago mentioned 
that those could be added at some point. "object" and "operator X" would 
be quite useful for me.

I tried to use constraints at some point in my code but they proven too 
limited in functionality - the biggest issue i faced was that i couldn't 
use a specialization with a forward declared class (which is the entire 
point of the forward declaration). For example i have a generic 
collection that only works on TSerializable subclasses so i gave it that 
constraint, but in another unit i need to define a specialization for it 
before it was defined (so the class was available with a forward class 
declaration) since that specialization also needs to be used by the 
class itself. In general it seems like having constrained generic work 
with a tree structure of derived types that use the generic itself is 
impossible.

Wouldn't storing a list of "specializations to confirm later in this 
unit" work (with later being when the class is actually defined in the 
unit)? The language already has forward declarations for a bunch of 
other things.

Kostas



More information about the fpc-devel mailing list