Recent

Author Topic: First look at advanced records  (Read 729 times)

TBMan

  • Sr. Member
  • ****
  • Posts: 346
First look at advanced records
« on: January 07, 2026, 02:02:01 am »
So I was curious and was talking to ChatgPt about OOP vs procedural coding and advanced records came into the conversation. I'm okay with using OOP and pointers in a GUI and "pixelating" tools, but for games I prefer procedural as I don't need un-freed pointers getting away from me. Advanced records look really interesting as I can encapsulate data without too much trouble.

One thing that chatgpt said I'd like to verify. If you have a record with a method in it, having an array of the records doesn't cause multiple duplicate methods in the executable. In other words, I'm not going to blow up the size of the program with this. Is this true?
Barry

Newest game (clone),
Missile Commander:
https://www.youtube.com/watch?v=tgKz0cxog-k

440bx

  • Hero Member
  • *****
  • Posts: 6081
Re: First look at advanced records
« Reply #1 on: January 07, 2026, 02:26:54 am »
One thing that chatgpt said I'd like to verify. If you have a record with a method in it, having an array of the records doesn't cause multiple duplicate methods in the executable. In other words, I'm not going to blow up the size of the program with this. Is this true?
Yes, it is true.

Advanced records don't even have a VMT because there are no virtual methods.  The called methods are all resolved at compile time.  Effectively, an advanced record, size-wise, behaves just like a normal record.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

TBMan

  • Sr. Member
  • ****
  • Posts: 346
Re: First look at advanced records
« Reply #2 on: January 07, 2026, 02:49:23 am »
One thing that chatgpt said I'd like to verify. If you have a record with a method in it, having an array of the records doesn't cause multiple duplicate methods in the executable. In other words, I'm not going to blow up the size of the program with this. Is this true?
Yes, it is true.

Advanced records don't even have a VMT because there are no virtual methods.  The called methods are all resolved at compile time.  Effectively, an advanced record, size-wise, behaves just like a normal record.

Ok, thank you!
Barry

Newest game (clone),
Missile Commander:
https://www.youtube.com/watch?v=tgKz0cxog-k

440bx

  • Hero Member
  • *****
  • Posts: 6081
Re: First look at advanced records
« Reply #3 on: January 07, 2026, 03:27:46 am »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

creaothceann

  • Sr. Member
  • ****
  • Posts: 261
Re: First look at advanced records
« Reply #4 on: January 07, 2026, 07:40:40 am »
Just like in "classic" OOP, the methods (procedures and functions) have a parameter that refers to a record instance:

Code: Pascal  [Select][+][-]
  1. type TProceduralRecord = record
  2.         a, b, c : integer;
  3.         end;
  4.  
  5.  
  6. procedure Init(var Instance : TProceduralRecord);  // explicit parameter
  7. begin
  8.         Instance.a := 0;
  9.         Instance.b := 1;
  10.         Instance.c := 2;
  11. end;
  12.  
  13.  
  14. procedure Test_1;
  15. var
  16.         p1, p2 : TProceduralRecord;
  17. begin
  18.         Init(p1);
  19.         Init(p2);
  20.         //...
  21. end;
  22.  
  23.  
  24. //------------------------------------------------------------------------------
  25.  
  26.  
  27. {$ModeSwitch AdvancedRecords}
  28.  
  29.  
  30. type TAdvancedRecord = record
  31.         a, b, c : integer;
  32.  
  33.         procedure Init;  // implicit parameter
  34.         end;
  35.  
  36.  
  37. procedure TAdvancedRecord.Init{(var Self : TAdvancedRecord)};
  38. begin
  39.         {Self.}a := 0;
  40.         {Self.}b := 1;
  41.         {Self.}c := 2;
  42. end;
  43.  
  44.  
  45. procedure Test_2;
  46. var
  47.         a1, a2 : TAdvancedRecord;
  48. begin
  49.         a1.Init;
  50.         a2.Init;
  51.         //...
  52. end;

Warfley

  • Hero Member
  • *****
  • Posts: 2038
Re: First look at advanced records
« Reply #5 on: January 07, 2026, 01:20:09 pm »
Advanced records are mostly syntactic sugar. Basically the following:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyRec = record
  3.     ...
  4.     procedure Foo;
  5.   end;

Is nothing other than:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyRec = record
  3.     ...
  4.   end;
  5.  
  6. procedure TMyRec$Foo(var Self: TMyRec);

Basically they give you all the "structure" of classes, i.e. different scopes (public, private), nested definitions, etc. without any of the runtime features or effects. Which means they are also much more lightweight and efficient than classes.

TBMan

  • Sr. Member
  • ****
  • Posts: 346
Re: First look at advanced records
« Reply #6 on: January 07, 2026, 04:59:39 pm »
Thanks everyone. This is interesting stuff. I'm going to experiment and do some things with it to see how it flows.
Barry

Newest game (clone),
Missile Commander:
https://www.youtube.com/watch?v=tgKz0cxog-k

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: First look at advanced records
« Reply #7 on: January 08, 2026, 09:23:32 am »
One of the most useful things is being able to implement a custom managed type hence guaranteeing that the variable will start off in a known state irrespective of where it's declared.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018