Recent

Author Topic: Access to the private fields and public properties of the class  (Read 4048 times)

woodhead

  • New member
  • *
  • Posts: 5
Hello.
I faced the following situation: the same code is compiled in Lazarus 2.0.8 and is not compiled in Lazarus 2.0.12.
Code: Pascal  [Select][+][-]
  1.   TColor = (red, green, blue);
  2.  
  3.   TShape = class
  4.   private
  5.     fColor: TColor;
  6.   public
  7.     property Color: TColor read fColor write fColor;
  8.   end;
  9.  
  10.   TShapeWrapper = class
  11.   private
  12.     fShape: TShape;
  13.   public
  14.     property Color: TColor read fShape.fColor write fShape.fColor; // there is an error in this line
  15.   end;
In Lazarus 2.0.12, error messages appear when trying to compile this code:
Quote
Compile Project, Target: project1.exe: Exit code 1, Errors: 2
unit1.pas(31,39) Error: Record or object type expected
unit1.pas(31,59) Error: Record or object type expected
Line 31 looks like this:
Code: Pascal  [Select][+][-]
  1. property Color: TColor read fShape.fColor write fShape.fColor;
All the code is located within a same module. Please tell me how to fix it?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Access to the private fields and public properties of the class
« Reply #1 on: July 16, 2021, 02:45:36 pm »
I don't know whether it will work but, have you tried with:
Code: Pascal  [Select][+][-]
  1. property Color: TColor read fShape.Color write fShape.Color;
so as to not access a private field?

If that doesn't work you'll have to use a getter/setter, I think.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

woodhead

  • New member
  • *
  • Posts: 5
Re: Access to the private fields and public properties of the class
« Reply #2 on: July 16, 2021, 02:50:04 pm »
For this line:
Code: Pascal  [Select][+][-]
  1. property Color: TColor read fShape.Color write fShape.Color;
get errors:
Quote
Compile Project, Target: project1.exe: Exit code 1, Errors: 4
unit1.pas(31,39) Error: Record or object type expected
unit1.pas(31,40) Error: Unknown record field identifier "Color"
unit1.pas(31,58) Error: Record or object type expected
unit1.pas(31,59) Error: Unknown record field identifier "Color"
I would not like to use getters/setters.

howardpc

  • Hero Member
  • *****
  • Posts: 3757
Re: Access to the private fields and public properties of the class
« Reply #3 on: July 16, 2021, 03:15:15 pm »
I would not like to use getters/setters.
Like it or not, if you want to use properties you have to respect the syntax. The FPC loophole by which you could read/write property values by direct access to the underlying field was closed some time ago.


lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Access to the private fields and public properties of the class
« Reply #5 on: July 16, 2021, 04:33:31 pm »
Yep, https://wiki.lazarus.freepascal.org/User_Changes_Trunk#Property_field_access_lists_no_longer_allows_classes
Thanks

But that is for trunk; if you're using a normal 2.0.12 release you've got FPC 3.2.0, which shouldn't have that change ... at least it isn't mentioned in User Changes 3.2.0 :-\
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 3064
Re: Access to the private fields and public properties of the class
« Reply #6 on: July 16, 2021, 05:03:58 pm »
Out of curiosity: it used to be possible with Delphi to "surface" items by subclassing... does that work with FPC?

In any event, fudging something like that would probably be more work than doing it properly using properties.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Kays

  • Sr. Member
  • ****
  • Posts: 357
  • Whasup!?
    • KaiBurghardt.de
Re: Access to the private fields and public properties of the class
« Reply #7 on: July 16, 2021, 05:18:21 pm »
Do you really need a “wrapper”? Doesn’t inheritance
Code: Pascal  [Select][+][-]
  1. TShapeWrapper = class(TShape)end;
do the same job?
Yours Sincerely
Kai Burghardt

woodhead

  • New member
  • *
  • Posts: 5
Re: Access to the private fields and public properties of the class
« Reply #8 on: July 17, 2021, 04:30:00 am »
But that is for trunk; if you're using a normal 2.0.12 release you've got FPC 3.2.0, which shouldn't have that change ... at least it isn't mentioned in User Changes 3.2.0 :-\
You are right. I thought so too.
Do you really need a “wrapper”? Doesn’t inheritance
Code: Pascal  [Select][+][-]
  1. TShapeWrapper = class(TShape)end;
do the same job?
This is a synthetic example to demonstrate the problem. I don't want to pile up inheritance where it can be made easier.
« Last Edit: July 17, 2021, 04:32:44 am by woodhead »

PascalDragon

  • Hero Member
  • *****
  • Posts: 3324
  • Compiler Developer
Re: Access to the private fields and public properties of the class
« Reply #9 on: July 17, 2021, 04:21:17 pm »
Yep, https://wiki.lazarus.freepascal.org/User_Changes_Trunk#Property_field_access_lists_no_longer_allows_classes
Thanks

But that is for trunk; if you're using a normal 2.0.12 release you've got FPC 3.2.0, which shouldn't have that change ... at least it isn't mentioned in User Changes 3.2.0 :-\

Seems like it was missed to move that entry to User Changes 3.2.0, I've done so now.

I would not like to use getters/setters.

Simply do use getters and setters and declare them as inline.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Access to the private fields and public properties of the class
« Reply #10 on: July 17, 2021, 06:37:32 pm »
Seems like it was missed to move that entry to User Changes 3.2.0, I've done so now.

Thanks, Dragon. It was a little baffling to have the info as if it were for trunk but the behaviour sneaking up in the release :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018