[fpc-devel] generic constraints...
Michael Van Canneyt
michael at freepascal.org
Tue Jul 29 09:58:44 CEST 2025
On Tue, 29 Jul 2025, Mattias Gaertner via fpc-devel wrote:
>>
>> And each list needs to be
>> TMyFinalItem1 =
>> specialize TFeatureXForList<
>> specialize TFeatureYForList<
>> specialize TFeatureZForList< specialize TFpgList<TItem1>
>> >
>> >
>> >;
>
> IMO that does not look human friendly for reading and debugging.
Let's hope it was a contrived example :-)
>
>
>> Currently I can swap the order, and git it working by creating a non
>> generic base.... But that introduces dependencies that were not wanted...
>>
>>
>> Also, the error by the compiler is
>> Error: Generics without specialization cannot be used as a type
>> for a variable
>>
>> But
>> generic FooChild<F: TFoo> = class(F)
>>
>> - F is not a variable
>> - TFoo in not a type (for somethnig), TFoo is a constraint
>
> The problem is the ambiguity of TFoo, because Delphi (aka modeswitch)
> distinguishes TFoo, TFoo<> and TFoo<,>:
>
> type
> generic TFoo<A> = class
> procedure Bar; virtual;
> end;
> generic TFoo<A,B> = class
> procedure NoBar; virtual;
> end;
>
> generic FooChild<F: TFoo> = class(F)
> procedure Bar; override;
> end;
>
> So you have to be more specific:
>
> generic FooChild<A,F: specialized TFoo<A>> = class(F)
> procedure Bar; override;
> end;
>
>
>> If that syntax was allowed, it should mean that
>> - F must be of any type that is (a descendand of) a specialization of
>> TFoo.
>>
>> So this is NOT to allow FooChild to be specialized with another generic,
>> but with a (any) specialization of that generic.
>
> AFAIK there is currently no "Any" placeholder in constraints. It would
> be something like
> generic FooChild<F: specialized TFoo<>> = ...
>
> Although that looks like something was forgotten. Maybe better:
>
> generic FooChild<F: specialized TFoo<*>> = ...
it seems to me that 'specialized' is not compatible with 'any',
as specialized needs a specific type: it actually creates the type.
So that would need to become something like
generic FooChild<F: TFoo<*>> = ..
Michael.
More information about the fpc-devel
mailing list