Recent

Author Topic: How to add new elements in a record Helper  (Read 490 times)

seany

  • Full Member
  • ***
  • Posts: 120
How to add new elements in a record Helper
« on: February 05, 2023, 06:31:46 pm »
Please consider this code:
Code: Pascal  [Select][+][-]
  1.     NodeStruct  = packed record
  2.       ID        : Integer;
  3.  
  4.       end;
  5.     TNodeArray = Array of NodeStruct;
  6.  
  7.     strNodePtr = ^stringNodeStruct ;
  8.     stringNodeStruct = record helper for NodeStruct
  9.       stringVal : String;
  10.       Parent    : strNodePtr;
  11.       Children  : Array of strNodePtr;
  12.       prev      : strNodePtr;
  13.       next      : strNodePtr;
  14.     end;
  15.              

This does not compile:

Error Message:
Error: An interface, helper or Objective-C protocol or category cannot contain fields

I also tried:

Code: Pascal  [Select][+][-]
  1. stringNodeStruct = packed record (NodeStruct)
  2.       stringVal : String;
  3.       Parent    : strNodePtr;
  4.       Children  : Array of strNodePtr;
  5.       prev      : strNodePtr;
  6.       next      : strNodePtr;
  7.     end;  
  8.  

Error: Fatal: Syntax error, "identifier" expected but "(" found

Well, that pretty much tells me everything . But, I would like to know, if it is possible to somehow "extend" or "inherit" a record, and then add more fields to it? In the worst case, I could always use Classes, but records would be nice. Thank you
My projects, among others:

https://linktr.ee/siderealNight

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to add new elements in a record Helper
« Reply #1 on: February 05, 2023, 06:49:24 pm »
Helper can extend existing type (i.e. class, record or simple type) with methods only. You cannot extend record nor class with a new field.
See: https://www.freepascal.org/docs-html/ref/refse64.html#x124-14800010.1
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to add new elements in a record Helper
« Reply #2 on: February 05, 2023, 06:59:31 pm »
Yes, classes are solution.
Or something like this:
Code: Pascal  [Select][+][-]
  1. NodeStruct  = packed record
  2.       ID        : Integer;
  3.       Extension: strNodePtr;
  4.     end;
Initialize Extenion to nil and eventually use
Code: Pascal  [Select][+][-]
  1. var NS: NodeStruct;
  2. begin
  3.  new(NS.Extension);
  4.   ...
  5.   dispose(NS.Extension);
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: How to add new elements in a record Helper
« Reply #3 on: February 06, 2023, 10:26:22 pm »
Well, that pretty much tells me everything . But, I would like to know, if it is possible to somehow "extend" or "inherit" a record, and then add more fields to it? In the worst case, I could always use Classes, but records would be nice. Thank you

Helper types can not add fields.

If you want to add additional fields either add the “parent” record as first field of the “descendant” or use types that support inheritance like TP-style object or Object Pascal-style class.

 

TinyPortal © 2005-2018