Recent

Author Topic: Advanced records public and private differences  (Read 1283 times)

440bx

  • Hero Member
  • *****
  • Posts: 5578
Advanced records public and private differences
« on: May 15, 2024, 04:34:29 am »
Hello,

I was reading the documentation to get a better idea of the difference between a field in an advanced record's public section and one in a private section.

The only difference I found is that fields accessed by record helper(s) for those records must be in the public section.

Is that the _only_ accessibility difference between public and private or are there other cases (not just being used in record helpers) where the private/public visibility makes a difference ?... if there is, I would much appreciate an example.

Thank you for your help.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

dseligo

  • Hero Member
  • *****
  • Posts: 1532
Re: Advanced records public and private differences
« Reply #1 on: May 15, 2024, 10:21:24 am »
Fields and methods declared in private section can't be used outside of unit where advanced record is declared.
And fields and methods declared in strict private section can't be used only inside of methods of advanced record.

440bx

  • Hero Member
  • *****
  • Posts: 5578
Re: Advanced records public and private differences
« Reply #2 on: May 15, 2024, 10:47:59 am »
Fields and methods declared in private section can't be used outside of unit where advanced record is declared.
IOW and, if I understood you correctly, fields in the private section are only visible in the unit in which the record is declared.  (similar to declaring a variable in the implementation section of a unit, it will only be visible in the unit and not outside of it.)

If you can confirm my understanding is correct (or not if it isn't), I would appreciate that.  Thank you.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

dseligo

  • Hero Member
  • *****
  • Posts: 1532
Re: Advanced records public and private differences
« Reply #3 on: May 15, 2024, 11:01:48 am »
Exactly.

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. uses unit1;
  6.  
  7. var a: TTestAdvRec;
  8.  
  9. begin
  10.   a.Prop1 := 10;
  11.   WriteLn(a.Func2);
  12.   //WriteLn(a.Func1); // can't be accessed
  13.   //a.Field1 := 10; // can't be accessed
  14.   //a.Field2 := 10; // can't be accessed
  15. end.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode ObjFPC}{$H+}
  4. {$modeswitch ADVANCEDRECORDS}
  5.  
  6. interface
  7.  
  8. type
  9.   TTestAdvRec = record
  10.   private
  11.     Field1: Integer;
  12.     function Func1: integer;
  13.   strict private
  14.     Field2: Integer;
  15.   public
  16.     property Prop1: Integer read Field1 write Field1;
  17.     function Func2: integer;
  18.   end;
  19.  
  20. function SomeFunction: Integer;
  21.  
  22. implementation
  23.  
  24. function TTestAdvRec.Func1: integer;
  25. begin
  26.   Result := 22;
  27. end;
  28.  
  29. function TTestAdvRec.Func2: integer;
  30. begin
  31.   Field2 := 2;
  32.   Result := 22;
  33. end;
  34.  
  35. function SomeFunction: Integer;
  36. var a: TTestAdvRec;
  37. begin
  38.   a.Field1 := 10;
  39.   //a.Field2 := 10; // strict private - can't be accessed here
  40.   Result := 22;
  41. end;
  42.  
  43. end.

440bx

  • Hero Member
  • *****
  • Posts: 5578
Re: Advanced records public and private differences
« Reply #4 on: May 15, 2024, 11:16:51 am »
Thank you dseligo.  That was very helpful. :)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018