[fpc-devel] Additional generic type constraints

Stefan Glienke sglienke at dsharp.org
Wed Feb 22 16:55:56 CET 2023


> I think Delphi basically has the same constraints as C#

Not quite (well maybe back then when they implemented them but C# improved since)- also they are subtly implemented differently.

In C# the constraint *class* means "a reference type" - in Delphi it actually means it has to be a class.

C# for a long time had similar issues with performing any kind of math in generics as Delphi or FPC have - the IComparer and IEqualityComparer interfaces where a huge kludge to be able to do generic collections.

Only C# 11 finally fixed this issue via static virtual members on interfaces (see https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/static-virtual-interface-members) so interfaces can be used as constraints and those static members are then being used when using operators and alike which enables all kinds of operations inside of generic code. A bit similar to C++ concepts (see https://en.cppreference.com/w/cpp/language/constraints)


More information about the fpc-devel mailing list