Recent

Author Topic: Record "inheritance"/structuring  (Read 1823 times)

quantumfoam

  • New Member
  • *
  • Posts: 23
Record "inheritance"/structuring
« on: August 21, 2022, 06:09:13 pm »
Is it possible to somehow write composite records to simplify record definition like this?

Code: Pascal  [Select][+][-]
  1. type rec1 = record
  2.   a:integer; // offset 0, length 4 bytes
  3. end;
  4.  
  5. type rec2 = record(rec1) // does not compile
  6.   b:integer; // offset 4, length 4 bytes
  7. end;
  8.  
  9. var x:rec2;
  10.  
  11. begin
  12.   x.a := 1;
  13.   x.b := 2;
  14. end.
  15.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 8127
Re: Record "inheritance"/structuring
« Reply #1 on: August 21, 2022, 06:26:31 pm »
Use a class.

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

quantumfoam

  • New Member
  • *
  • Posts: 23
Re: Record "inheritance"/structuring
« Reply #2 on: August 21, 2022, 06:34:56 pm »
@MarkMLl. This would be too slow for my purposes, since each object needs a create call. I need to allocate many of such records into pages, and access them via pointers.
EDIT: Also, I don't need any methods/functions attached to these records. Just plain data.
« Last Edit: August 21, 2022, 06:37:31 pm by quantumfoam »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Record "inheritance"/structuring
« Reply #3 on: August 21, 2022, 06:38:45 pm »
@MarkMLl. This would be too slow for my purposes, since each object needs a create call. I need to allocate many of such records into pages, and access them via pointers.
use objects then
a quick feature comparison
https://wiki.freepascal.org/Record#Records_compared_to_other_structured_types

quantumfoam

  • New Member
  • *
  • Posts: 23
Re: Record "inheritance"/structuring
« Reply #4 on: August 21, 2022, 07:42:57 pm »
@HeavyUser, thanks. I didn't know about Object, I only used Classes before. And I didn't know that class can be "created" via New keyword. Anyway, since Objects can be created in arrays without instantion/creation of every single instance (contrary to Classes), I'll use Objects.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. type rec2 = record
  6.   a,b,c:integer;
  7. end;
  8. type prec2 = ^rec2;
  9.  
  10. type obj1 = object
  11.   a,b:integer;
  12. end;
  13.  
  14. type obj2 = object(obj1)
  15.   c:integer;
  16.   procedure add;
  17. end;
  18. type pobj2 = ^obj2;
  19.  
  20. type cls1 = class
  21.   a,b:integer;
  22. end;
  23. type cls2 = class(cls1)
  24.   c:integer;
  25.   procedure add;
  26. end;
  27. type pcls2 = ^cls2;
  28.  
  29.  
  30. procedure obj2.add;
  31. begin
  32.   c := c + 4;
  33. end;
  34.  
  35. procedure cls2.add;
  36. begin
  37.   c := c + 4;
  38. end;
  39.  
  40. var o:pobj2; c:pcls2;
  41.   page:array of obj2;
  42.   i:integer;
  43.   const cnt = 3;
  44.  
  45.  
  46.  
  47. begin
  48.   writeln(sizeof(rec2)); // 12
  49.   writeln(sizeof(obj2)); // 12
  50.   writeln(sizeof(cls2)); // 4
  51.   setlength(page, cnt);
  52.   for i := 0 to cnt - 1 do begin
  53.     page[i].c := i;
  54.     page[i].add;
  55.     writeln(page[i].c);
  56.   end;
  57.   //new(o);
  58.   //new(c);
  59. end.
  60.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 5815
  • Compiler Developer
Re: Record "inheritance"/structuring
« Reply #5 on: August 22, 2022, 09:15:00 am »
And I didn't know that class can be "created" via New keyword.

  • Classes can not be instantiated via New, they are only instantiated by using a constructor
  • New is not a keyword; it's an intrinsic and works on any typed pointer variable

Adriaan van Os

  • Newbie
  • Posts: 4
Re: Record "inheritance"/structuring
« Reply #6 on: December 07, 2024, 04:48:14 pm »
And I didn't know that class can be "created" via New keyword.

  • Classes can not be instantiated via New, they are only instantiated by using a constructor
  • New is not a keyword; it's an intrinsic and works on any typed pointer variable

Classes can very well be instantiated via New or GetMem or malloc or whatever; macpas mode doesn't even have constructors. Have a look at TObject in the system unit.

Regards,

Adriaan van Os

MarkMLl

  • Hero Member
  • *****
  • Posts: 8127
Re: Record "inheritance"/structuring
« Reply #7 on: December 07, 2024, 05:06:20 pm »
Classes can very well be instantiated via New or GetMem or malloc or whatever; macpas mode doesn't even have constructors. Have a look at TObject in the system unit.

I would suggest not arguing with PascalDragon, unless you want to make an enormous fool of yourself very quickly.

Sven is one of the senior developers, and if he says "do this" or "don't do this" you listen: what you /don't/ do is tell him to read the documentation.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 16407
  • Censorship about opinions does not belong here.
Re: Record "inheritance"/structuring
« Reply #8 on: December 07, 2024, 05:22:24 pm »
Mark, Adriaan is a very old hand and not a noob, to the contrary. He just forgot that allocated is not the same as instantiated. I would blame that on the festive calendar.
PascalDragon would usually take comments from him serious.
Although here he is obviously blatantly wrong. He mixed object - belonging to our period - with TObject. ;D
« Last Edit: December 07, 2024, 05:31:10 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5815
  • Compiler Developer
Re: Record "inheritance"/structuring
« Reply #9 on: December 09, 2024, 10:31:25 pm »
And I didn't know that class can be "created" via New keyword.

  • Classes can not be instantiated via New, they are only instantiated by using a constructor
  • New is not a keyword; it's an intrinsic and works on any typed pointer variable

Classes can very well be instantiated via New or GetMem or malloc or whatever; macpas mode doesn't even have constructors. Have a look at TObject in the system unit.

For Delphi-style classes this is only allowed for MacPas mode (and as that is a very niche mode this is not relevant for the majority of discussions here). In all other modes that support Delphi-style class types a call to the constructor must be used.

Although here he is obviously blatantly wrong. He mixed object - belonging to our period - with TObject. ;D

Adriaan is in so far right that it works this way in mode MacPas where object is in fact a Delphi-style class, not a TP-style one.
« Last Edit: December 09, 2024, 10:33:41 pm by PascalDragon »

Thaddy

  • Hero Member
  • *****
  • Posts: 16407
  • Censorship about opinions does not belong here.
Re: Record "inheritance"/structuring
« Reply #10 on: December 10, 2024, 09:54:38 am »
I see. So only 68000 style macpas. Did not know that.
There is nothing wrong with being blunt. At a minimum it is also honest.

 

TinyPortal © 2005-2018