[fpc-pascal] Records as properties and Delphi compiler error

fpclist at silvermono.co.za fpclist at silvermono.co.za
Sat Jun 6 17:36:57 CEST 2009


Hi Guys,

Is there a reason why the following code fails to compile in Delphi but 
compile in FPC? Could the reason be that FPC allows the use of global 
properties? Tested with Delphi 5 and up.

The Delphi compiler complains about the lines:
TestClass.TestRec.X := 10;
TestClass.TestRec.Y := 20; within the button click handler.
Delphi Error:  "Left side cannot be assigned to"

//*******************************************************
unit Unit1; 

(*
    This code sample is written for use with Lazarus.
    The Delphi equivalent fails to compile using Delphi.
*)

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1 : TButton;
    procedure Button1Click(Sender : TObject);
  private
  public
  end;

  TTestRec = record
    X : Byte;
    Y : Byte;
  end;

  TTestClass = class
  private
    fTestRec : TTestRec;
  public
    property TestRec : TTestRec read fTestRec write fTestRec;
  end;

var
  Form1 : TForm1; 

implementation

procedure TForm1.Button1Click(Sender : TObject);
var
  TestClass : TTestClass;

begin
  TestClass := TTestClass.Create();
  try
    ShowMessage('TestRec before' + #13 +
                'X = ' + IntToStr(TestClass.TestRec.X) + #13 +
                'Y = ' + IntToStr(TestClass.TestRec.Y));

    TestClass.TestRec.X := 10; //* Error "Left side cannot be assigned to"
    TestClass.TestRec.Y := 20 ;//* Error "Left side cannot be assigned to"

    ShowMessage('TestRec after' + #13 +
                'X = ' + IntToStr(TestClass.TestRec.X) + #13 +
                'Y = ' + IntToStr(TestClass.TestRec.Y));

  finally
    FreeAndNil(TestClass);
  end;
end;

initialization
  {$I unit1.lrs}

end.




More information about the fpc-pascal mailing list