[fpc-pascal] Class field property access

Ryan Joseph genericptr at gmail.com
Fri Jun 7 13:41:23 CEST 2019



> On Jun 7, 2019, at 2:51 AM, Michael Van Canneyt <michael at freepascal.org> wrote:
> 
> What kind of behaviour are you referring to.

Something wrong with my mail server causing your server to block me (Jonas said my server didn’t handle greylisting correctly). Easier to just re-sub using gmail for now.

> 
>> Who cares if there is an additional level of indirection?
> 
> Because the indirection may not be there. The compiler cannot guarantee that, thus leading to access violations.
> 
> You can use a getter function to get the desired property. In the getter you
> can decide what to do: create the b instance (sometimes a valid choice) or raise an error.
> 
> But the compiler cannot take this decision for you, so b.x is not allowed.

I really have to question the wisdom of this. The code below works in 3.0.4 and is completely safe. I understand there was delphi compatibility and problems with published properties however but that could have been worked around easily. Maybe it’s a not big deal in the grand scheme of things but writing boiler plate like getters/setters is not fun.

type
TB = class
  x: integer;
end;

type
 TA = class
  private
    b: TB;
  public
    property x: integer read b.x write b.x;
    procedure AfterConstruction; override;
end;


procedure TA.AfterConstruction;
begin
 b := TB.Create;
end;

var
 a: TA;
begin
 a := TA.Create;
 a.x := 100;
 writeln(a.x);
end.

Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list