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

Martin Frb lazarus at mfriebe.de
Mon Nov 24 21:30:25 CET 2025


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?


unit Unit1;{$mode ObjFPC}
interface
type
   generic TFoo<a> = class F: A; end;
implementation
end.



program Project1;{$Mode objfpc}
uses Unit1;
type
   TFoo = class end;
   //TFoo = word;
   TBar = specialize TFoo<byte>;  // Works, ignores the class (or word) TFoo
begin
end.


############# Example 2  ##############


unit Unit1;{$mode ObjFPC}
interface
type
TFoo = class end;   // TFoo that could be base class
implementation
end.


program Project1;{$Mode objfpc}
uses Unit1;
type
   TFoo = word;
   TFoo2 = class(TFoo) end;  // <<<<<< Fails / does not see the class TFOO

begin
end.



More information about the fpc-devel mailing list