[fpc-devel] Bug in FPC and declaring distinct types

Ivo Steinmann ivo_steinmann at gmx.net
Thu Sep 18 12:43:08 CEST 2008


Graeme Geldenhuys schrieb:
> Hi,
>
> I was following a discussion in the delphi.non-technical newsgroup.
> They raised an issue about distinct types. I tried the following
> example under FPC and unexpectedly, FPC doesn't raise any errors!
>
> If you declare a distinct type as follows:
>   type TMyInteger = type Integer;
>
> The type TMyInteger <> Integer so you are not supposed to be allowed
> to assign one to the other. Yet you can. See the following program.
>
> ----------------------[  test1.pas ]--------------------------
> program test1;
>
> {$mode objfpc}{$H+}
>
> uses
>   Classes, SysUtils;
>
> type
>   Angle = type Double;
>   Length = type Double;
>
> function AngleToLength(var a: Angle):Length;  { Note the var parameter }
> begin
>   result := a;
> end;
>
> function AngleToLength2(a: Angle):Length;  { Here is NO var parameter }
> begin
>   result := a;
> end;
>
> procedure Test;
> var
>   a: Angle;
>   l: Length;
>   d: Double;
> begin
>   d := Now;
>   a := d;
>   l := a;  // Should this have failed ????
>
>   { FPC raises no errors, but at least Delphi raises an error wit the
>      var parameter type. }
>   a := AngleToLength(l);  // This should definitely have failed!!
>   a := AngleToLength2(l);  // Should this fail too?
> end;
>
> begin
>   Test;
> end.
> --------------------------[ end ]-----------------------------
>
> The program was compiled with FPC 2.2.3:
>    fpc test1.pas
>    ./test1
>
>
> Regards,
>  - Graeme -
>
>
>   


This one is working also ;) No idea if it should or not....


program test;

{$mode objfpc}
{$H+}

type
  TTypeA = type Integer;
  TTypeB = type Integer;

  IIntf = interface
    function GetSomething: TTypeA;
  end;

  TMyObject = class(TInterfacedObject, IIntf)
    function GetSomething: TTypeB; virtual;
  end;

  TMyObject2 = class(TMyObject)
    function GetSomething: TTypeA; override;
  end;

function TMyObject.GetSomething: TTypeB;
begin
end;

function TMyObject2.GetSomething: TTypeA;
begin
end;

begin
end.




More information about the fpc-devel mailing list