[fpc-devel] Additional generic type constraints

Sven Barth pascaldragon at googlemail.com
Wed Feb 22 14:11:58 CET 2023


Kostas Michalopoulos via fpc-devel <fpc-devel at lists.freepascal.org> schrieb
am Mi., 22. Feb. 2023, 10:37:

> > 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.
>

Some additional constraints might be added. However I have none of them
planned currently and enough other things to do.


> 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.
>

Forward declarations can not be used for constraints, because a
specialization of the generic might be used before the forward declared
type is fully defined and the compiler *must* be able to check whether the
parameter in question is compatible at the time the specialization is
declared.

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20230222/a01a34ef/attachment.htm>


More information about the fpc-devel mailing list