[fpc-devel] generics defy scoping rules -- Intended?

Michael Van Canneyt michael at freepascal.org
Tue Nov 25 10:48:52 CET 2025



On Mon, 24 Nov 2025, Martin Frb via fpc-devel wrote:

> Normally, if I refer to a type then the first type found by the name 
> will be taken => and if that type is not compatible, then I get an error.
> => See Example 2
>
> But, if I try to specialize a generic, and some other type by the same 
> name exists, and comes first in the search order, then it will be ignored.
>
> The specialize below, ignores the "TFoo = class"  which can not be used, 
> and finds the TFoo from unit1.
>
> Is that intended?

Delphi allows to overload a generic with a non-generic class.

TFoo = class end;
TFoo <a> = class F: a end;

FPC has experimental support for this as far as I know.

So the answer would be: 
Yes, the specialize will look for a generic definition, ignoring classes
with the same name that are not generic.

Michael.


More information about the fpc-devel mailing list