Recent

Author Topic: Better support for records and properties  (Read 2255 times)

domasz

  • Sr. Member
  • ****
  • Posts: 455
Better support for records and properties
« on: April 05, 2024, 10:24:40 pm »
Code: Pascal  [Select][+][-]
  1. type TPix = record
  2. R,G,B,A: Byte;
  3. end;
  4.  
  5. property Pixel[X,Y: Integer]: TPix read GetPixel write SetPixel; default;

Now I can do:

Code: Pascal  [Select][+][-]
  1. R := MyBitmap[x,y].R;

but I can't:

Code: Pascal  [Select][+][-]
  1. MyBitmap[x,y].R := R;

Would be nice to be able to do that.

Also these 2 compile but don't work:
Code: Pascal  [Select][+][-]
  1. with MyBitmap[x,y] do R := 255;
  2.  
  3. MyBitmap[x,y].R += 20;
« Last Edit: April 05, 2024, 11:17:18 pm by domasz »

ASerge

  • Hero Member
  • *****
  • Posts: 2262
Re: Better support for records and properties
« Reply #1 on: April 06, 2024, 03:31:13 am »
Would be nice to be able to do that.
Assuming that this code works, what does it output?
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$LONGSTRINGS ON}
  3. {$APPTYPE CONSOLE}
  4.  
  5. type
  6.   TRec = record
  7.     X: Integer;
  8.   end;
  9.  
  10. function GetRec: TRec;
  11. begin
  12.   Result.X := 1;
  13. end;
  14.  
  15. var
  16.   R: TRec = (X: 2);
  17. begin
  18.   GetRec.X := 3;
  19.   Writeln(GetRec.X);
  20.   Readln;
  21. end.

This is the reason why the compiler does not allow you to do this.

domasz

  • Sr. Member
  • ****
  • Posts: 455
Re: Better support for records and properties
« Reply #2 on: April 06, 2024, 06:40:35 am »
I see your point but in this case it could just discard the result (nothing to write value back to) while in my case there is something to write value back to.

ASerge

  • Hero Member
  • *****
  • Posts: 2262
Re: Better support for records and properties
« Reply #3 on: April 06, 2024, 07:16:20 am »
I see your point but in this case it could just discard the result (nothing to write value back to) while in my case there is something to write value back to.
The same.
Properties with the read method work like a regular function. To avoid side effects for functions that return a "complex" type, the compiler creates a temporary variable. Therefore, the following operations will be applicable to it.
In early versions of Delphi, this was allowed, but the developers wondered why the result was rejected. Then just banned it.
For this to work, a pointer must be returned, in particular an object of the class.

cdbc

  • Hero Member
  • *****
  • Posts: 1165
    • http://www.cdbc.dk
Re: Better support for records and properties
« Reply #4 on: April 06, 2024, 07:25:17 am »
Hi
Wouldn't it be possible to return a "PPix" and then {$modeswitch autoderef on}, if you don't like the ^-hat-business...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

domasz

  • Sr. Member
  • ****
  • Posts: 455
Re: Better support for records and properties
« Reply #5 on: April 06, 2024, 11:44:18 am »
Thank you, ASerge and Benny!

 

TinyPortal © 2005-2018