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

Michael Van Canneyt michael at freepascal.org
Thu Aug 17 20:08:10 CEST 2023



On Thu, 17 Aug 2023, Mattias Gaertner via fpc-devel wrote:

> Hi,
>
> FPC and Delphi handle type alias differently and I wonder if this was 
> deliberate.
>
> type
>  TMyDouble = type double;
>
>  TBird<T> = class
>    a: T;
>  end;
>  TDoubleBird = TBird<double>;
>  TMyDoubleBird = TBird<TMyDouble>;
>
> 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 ?

In Delphi

Type
   TArrayInteger1 = Array of Integer;
   TArrayInteger2 = Array of Integer;

var
   A1 : TArrayInteger1;
   A2 : TArrayInteger2;

begin
   A1:=A2;
end.

Will not work, but in FPC it does. This is IMO similar.

Of course, the method addresses are different. Other than that and the
different RTTI (which has 100% the same structure) the classes are
identical. So based on that we could argue they are not assignment
compatible.

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

Michael.



More information about the fpc-devel mailing list