Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2023-06

Error: Argument cannot be assigned to

11 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Juha Manninen

Why the following code fails to compile?

type
  TMyRec = record
    ss: String;
    ii: Integer;
  end;
  TMyClass = class
  private
    fName: String;
    fRecInstance: TMyRec;
  property
    RecInstance: TMyRec read fRecInstance;// write fRecInstance;
  end;
var
  MyObj : TMyClass;
begin
  MyObj := TMyClass.Create;
  MyObj.RecInstance.ii := 123;
  MyObj.Free;
end.

Access through property seems to be the problem. Accessing fRecInstance directly works.

Juha

Juha Manninen
to Juha Manninen

Formatting was a little weird. This is what I meant, but the error remains :

type
  TMyRec = record
    ss: String;
    ii: Integer;
  end;
  TMyClass = class
  private
    fName: String;
    fRecInstance: TMyRec;
  public
    property RecInstance: TMyRec read fRecInstance;// write fRecInstance;
  end;
var
  MyObj : TMyClass;
begin
  MyObj := TMyClass.Create;
  MyObj.RecInstance.ii := 123;
  MyObj.Free;
end.
Martin Frb
to Juha Manninen: Why the following code fails to compile? type TMyRec = record ss: String; ii: Integer;…
Juha Manninen wrote
Why the following code fails to compile?   TMyRec = record

...

property     RecInstance: TMyRec read fRecInstance;// write fRecInstance;

...

MyObj.RecInstance.ii := 123; Access through property seems to be the problem. Accessing fRecInstance directly works.

A property enforces that any code you write will work even if you change the property to a getter/setter.
Or in other words, it will fail any code that would not work.

If this was a getter, it would be
   function GetRec: TMyRec;

And that returns a *copy* in a temporary location of the record. Technically you can modify the members of the temp record (just to have the work thrown away afterwards).

So I guess the compiler forbids it, because the only case it would ever be done is: by mistake.

Mattias Gaertner
to Juha Manninen: Why the following code fails to compile? type TMyRec = record ss: String; ii: Integer;…
Show quoted text 24 lines
Why the following code fails to compile? type TMyRec = record ss: String; ii: Integer; end; TMyClass = class private fName: String; fRecInstance: TMyRec; property RecInstance: TMyRec read fRecInstance;// write fRecInstance; end; var MyObj : TMyClass; begin MyObj := TMyClass.Create; MyObj.RecInstance.ii := 123; MyObj.Free; end. Access through property seems to be the problem. Accessing fRecInstance directly works.

Correct. Property RecInstance is read only.

Mattias

Juha Manninen
to Mattias Gaertner

On Sunday, June 4, 2023, Mattias Gaertner via fpc-pascal <

Show quoted text 5 lines
wrote: Correct. Property RecInstance is read only.

No, I can define it as :

    property RecInstance: TMyRec read fRecInstance write fRecInstance;

and still get the same error.

Juha

Martin
to Juha Manninen: Why the following code fails to compile? type TMyRec = record ss: String; ii: Integer;…
Juha Manninen wrote
Why the following code fails to compile?   TMyRec = record

...

property     RecInstance: TMyRec read fRecInstance;// write fRecInstance;

...

MyObj.RecInstance.ii := 123; Access through property seems to be the problem. Accessing fRecInstance directly works.

A property enforces that any code you write will work even if you change the property to a getter/setter.
Or in other words, it will fail any code that would not work.

If this was a getter, it would be
   function GetRec: TMyRec;

And that returns a *copy* in a temporary location of the record. Technically you can modify the members of the temp record (just to have the work thrown away afterwards).

So I guess the compiler forbids it, because the only case it would ever be done is: by mistake.

Martin Frb
to Martin
Martin wrote 8 lines
On 04/06/2023 15:04, Juha Manninen via fpc-pascal wrote: > Why the following code fails to compile? > MyObj.RecInstance.ii := 123; Technically you can modify the members of the temp record (just to have the work thrown away afterwards). So I guess the compiler forbids it, because the only case it would ever be done is: by mistake.

Btw, this works (or actually not, it works wrong)

   with  MyObj.RecInstance do {begin} ii := 123; {end;}

Now you could do some work with the temp result, and have it trashed at the end of the "with" block.

However, fpc incorrectly changes the value in the original record. Yet, don't use it. It will (likely) change, and stop working. (or work correctly, changing the temp value only).

Sven Barth
to Martin Frb: Btw, this works (or actually not, it works wrong)   with  MyObj.RecInstance do {begin}…

Martin Frb via fpc-pascal schrieb am So.,
4. Juni 2023, 18:10:

Show quoted text 21 lines
On 04/06/2023 17:49, Martin via fpc-pascal wrote: > On 04/06/2023 15:04, Juha Manninen via fpc-pascal wrote: >> Why the following code fails to compile? >> MyObj.RecInstance.ii := 123; > Technically you can modify the members of the temp record (just to > have the work thrown away afterwards). > > So I guess the compiler forbids it, because the only case it would > ever be done is: by mistake. Btw, this works (or actually not, it works wrong) with MyObj.RecInstance do {begin} ii := 123; {end;} Now you could do some work with the temp result, and have it trashed at the end of the "with" block. However, fpc incorrectly changes the value in the original record. Yet, don't use it. It will (likely) change, and stop working. (or work correctly, changing the temp value only).

Correct. This *will* be fixed also. There already is a bug report about it.

Regards,
Sven

Michael Van Canneyt
to Juha Manninen: On Sunday, June 4, 2023, Mattias Gaertner via fpc-pascal < No, I can define it as :…
Juha Manninen wrote 10 lines
On Sunday, June 4, 2023, Mattias Gaertner via fpc-pascal < > wrote: > > Correct. Property RecInstance is read only. > No, I can define it as : property RecInstance: TMyRec read fRecInstance write fRecInstance; and still get the same error.

Because properties can have getters and setters, it follows that you can only read/write a property as a whole, never a part.

What you will do in your case is read the property in a temp location, change the temp location and that's it. The original location is unmodified.

There is a bug in fpc (as mentioned by Sven and Martin) that violates this principle, but as Sven said, this will be fixed.

Michael.