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

Graeme Geldenhuys graemeg.lists at gmail.com
Thu Sep 18 09:08:04 CEST 2008


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 -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/



More information about the fpc-devel mailing list