View Issue Details
|ID||Project||Category||View Status||Date Submitted||Last Update|
|0020854||FPC||Compiler||public||2011-12-09 10:50||2018-02-08 18:17|
|Reporter||Marco van de Voort||Assigned To||Sven Barth|
|Priority||normal||Severity||feature||Reproducibility||have not tried|
|Fixed in Version||3.0.0|
|Summary||0020854: Generic constraints|
|Description||Delphi 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 Revision||23158|
basepool.pas (3,195 bytes)
Constraints are already on my ToDo list. I just don't know whether I first implement constraints or generic functions :)
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.
||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.|
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.
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.
Please test and close if okay.
|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|