View Issue Details

IDProjectCategoryView StatusLast Update
0020854FPCCompilerpublic2018-02-08 18:17
ReporterMarco van de Voort Assigned ToSven Barth  
PrioritynormalSeverityfeatureReproducibilityhave not tried
Status closedResolutionfixed 
Fixed in Version3.0.0 
Summary0020854: Generic constraints
DescriptionDelphi allows to specify constraints with generics.

type xx<t:constraint1,constraint2> = class ...

Constraints can be classtypes, interfacetypes and special cases "class","record" and "constructor", separated with comma's like interfaces.

Constructor means that the class has a constructor without parameters, and allows generic code to call a constructor on a type T.

Class means that it is a reference type (class or interface), probably important for typecasting relating to inheritance.

record means it is a value type
Fixed in Revision23158
Attached Files


related to 0019491 resolvedJonas Maebe A lot of Generics' implementations 


2011-12-09 10:50


basepool.pas (3,195 bytes)

Sven Barth

2011-12-09 14:08

manager   ~0054865

Constraints are already on my ToDo list. I just don't know whether I first implement constraints or generic functions :)


Marco van de Voort

2011-12-09 14:58

manager   ~0054870

Since I don't use generic functions yet, I vote for constraints :-)

Though the images bit (from which the alias bugreports are extracted) is more important to me than this pool stuff.

Alexander Shishkin

2011-12-09 23:53

reporter   ~0054880

I think that generic functions are more priority. Delphi code with constrains can be compiled with FPC using ifdefs around them, and will work as expected (!) But I have no idea now to make code with generic functions portable to fpc without being fully rewritten.

Anthony Walter

2012-05-13 00:14

reporter   ~0059544

Adding constraints won't improve functionality.

Constraints would be nice, but the way FPC handles generics in some ways is much superior to the current Delphi implementation or even the C# implementation, in my opinion.

I think the way FPC generics work now is like C++ templates, which are code generators. The generated code is checked against the types at compile time allowing us to write something like:

function TMyClass<T>.Add(const A, B: T): T;
  Result := A + B;

The above is not possible in Delphi or C#, but the above is HIGHLY desirable. It's exactly because the code above won't work in Delphi or C# why those languages have generic constraints. The constraints relax those compilers to allow call specific methods of T to be invoked. But currently FPC generics will figure it methods of T exist at compile time without the need for constraints and this is a good thing.

As such, the only reason I see to add constraints is to make FPC code more compatible with Delphi.

Sven Barth

2012-05-14 12:49

manager   ~0059578

Constraints are useful if you have certain expectations about the type. Consider FPC's TFPGObjectList. Currently you can specialize it using every type, but once constraints are added the declaration can be changed to "TFPGObjectList<T: class> = class ... end;" so that it is clear that only descendants of TObject can be used for a specialization (the compiler will enforce this). This also allows the compiler to do more checks while parsing the generic instead of when specializing.


Sven Barth

2012-12-16 15:11

manager   ~0064334

Please test and close if okay.


Issue History

Date Modified Username Field Change
2011-12-09 10:50 Marco van de Voort New Issue
2011-12-09 10:50 Marco van de Voort File Added: basepool.pas
2011-12-09 10:50 Marco van de Voort FPCOldBugId => 0
2011-12-09 14:08 Sven Barth Note Added: 0054865
2011-12-09 14:58 Marco van de Voort Note Added: 0054870
2011-12-09 22:43 Florian Severity minor => feature
2011-12-09 23:53 Alexander Shishkin Note Added: 0054880
2011-12-11 11:48 Paul Ishenin Tag Attached: generics
2012-05-09 16:44 Marco van de Voort Description Updated
2012-05-13 00:14 Anthony Walter Note Added: 0059544
2012-05-14 12:49 Sven Barth Note Added: 0059578
2012-06-01 14:14 Sven Barth Relationship added related to 0019491
2012-06-01 14:15 Sven Barth Status new => acknowledged
2012-12-16 15:11 Sven Barth Fixed in Revision => 23158
2012-12-16 15:11 Sven Barth Status acknowledged => resolved
2012-12-16 15:11 Sven Barth Fixed in Version => 2.7.1
2012-12-16 15:11 Sven Barth Resolution open => fixed
2012-12-16 15:11 Sven Barth Assigned To => Sven Barth
2012-12-16 15:11 Sven Barth Note Added: 0064334
2018-02-08 18:17 Marco van de Voort Status resolved => closed