[fpc-devel] Question about generics / what is checked, and what not -- and why?

Martin Frb lazarus at mfriebe.de
Sun Oct 19 17:50:42 CEST 2025


On 19/10/2025 17:33, Hairy Pixels via fpc-devel wrote:
> On Oct 19, 2025 at 10:27:50 PM, Martin Frb via fpc-devel 
> <fpc-devel at lists.freepascal.org> wrote:
>> Given an non-constrained param, then (in code) any member is assumed to
>> be ok.
>> But in declarations, members of the param can only be used if the
>> compiler knows them.
>>
>>   generic TGenA<T1,T2> = class
>>     X: T1;
>>     Y: T2.unknown;
>>   end;
>>
>> What reason may there be to reject the line for Y?
>
> I agree the compiler can not know that T2.unknown is a valid member 
> before specializing so it should be valid syntax.
>
> I think it's just a limitation in the parser. Ideally some subset of 
> typeless syntax should be allowed during the initial parse and only 
> only validated once the the type is specialized.


Well, yes, it may be unimplemented in the parser... And maybe that is 
all of it.

Maybe historically it would have been a complex task to add. Yet, 
currently it would seem to be that the parser just needs to skip any 
amount of "dot identifier"?

I though, if it may be some internal representation (nodes) generated by 
the parser, that must later (at specialization) match. But then I can 
have the base class

{advanced records}
TClassA = class
   public type record
     public type TUnknown: record end;
   end;
end;

and replace it by
TClassA = class
   public type class
     public type TUnknown: byte;
   end;
end;

which surely generates something different?

But then, of course I don't know the compiler internals...
So hence I am asking...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20251019/8e09c496/attachment.htm>


More information about the fpc-devel mailing list