[fpc-devel] specialization with type alias, bug or feature

Mattias Gaertner nc-gaertnma at netcologne.de
Fri Aug 18 00:09:14 CEST 2023

On 17.08.23 20:08, Michael Van Canneyt wrote:
> [...]
>> Both FPC and Delphi create different classes for TDoubleBird and 
>> TMyDoubleBird.
>> In Delphi they are not assign compatible, in FPC they are. For example:
>> var
>>  a: TDoubleBird;
>>  b: TMyDoubleBird;
>> begin
>>  b:=TMyDoubleBird.Create;
>>  a:=b; // forbidden in Delphi
>>  writeln(a is TDoubleBird); // writes false
>> Bytewise the assignment is ok, but logic wise "a" is no longer a 
>> TDoubleBird.
>> Is this a bug or a feature?
> Depends on how you look at it. Double and TMyDouble are assignment 
> compatible.
> There is no reason why the same should not hold true for TDoubleBird and 
> TMyDoubleBird ?

I guess that is true for specialized arrays, records (non advanced), 
proc types and pointers, unless I forgot something (maybe helpers?).
But classes and interfaces have "is", "as", "class of", and "class 
vars", where the difference matter.

> Of course, the method addresses are different. Other than that and the
> different RTTI (which has 100% the same structure)

The different RTTI has 100% the same structure?
For clarification for others to follow the discussion here is an example:

   TBird<T> = class(TPersistent)
     FWing: T;
     property Wing: T read FWing;

This is allowed in Delphi, but FPC refuses generic templates in published.
The TBird<double>.Wing has a different PPropInfo than 
TBird<TMyDate>.Wing. That is probably only relevant in some rare cases.

> I am inclined to go for feature, but maybe there are arguments to tip the
> balance in the direction of bug.

One point where it could wrong, is when you port Delphi code to FPC.
For example this is fine in Delphi:

   TBird<T> = class
     class var Cnt: integer;
   TDoubleBird = TBird<double>;

procedure Fly(b: TDoubleBird);
   inc(b.Cnt); // under Delphi this must be TBird<double>.Cnt
     // not so under FPC
   if TDoubleBird.Cnt=1 then ...


More information about the fpc-devel mailing list