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

Michael Van Canneyt michael at freepascal.org
Thu Sep 18 09:42:39 CEST 2008



On Thu, 18 Sep 2008, Graeme Geldenhuys wrote:

> 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.

But obviously delphi also allows it ? It compiles everything,
just not as a var parameter.

The whole 'Type' thing is a bit ridiculous: the only reason they did it
is so they can have different type pointers in the RTTI for something which
is the same 'type' anyway. For the rest it is extremely badly designed, 
and not really consistent. It serves no purpose. FPC introduced it to 
be able to compile Delphi code, no other reason.

Michael.

> 
> ----------------------[  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 -
> 
> 
> _______________________________________________
> fpGUI - a cross-platform Free Pascal GUI toolkit
> http://opensoft.homeip.net/fpgui/
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel
> 



More information about the fpc-devel mailing list