Recent

Author Topic: Using the record type inside a self-declaration  (Read 3874 times)

ASerge

  • Hero Member
  • *****
  • Posts: 2494
Using the record type inside a self-declaration
« on: July 02, 2024, 11:13:18 pm »
I didn't even know it was possible. From which version?
Code: Pascal  [Select][+][-]
  1. type
  2.   TRec = record
  3.     FRecs: array of TRec;
  4.     FOther: Integer;
  5.   end;
« Last Edit: July 03, 2024, 12:41:09 am by ASerge »

VisualLab

  • Hero Member
  • *****
  • Posts: 731
Re: Using the record type inside a self-declaration
« Reply #1 on: July 02, 2024, 11:24:41 pm »
I checked too. Works. The magic of pointers.

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: Using the record type inside a self-declaration
« Reply #2 on: July 03, 2024, 11:20:37 am »
As far as I know this was syntactically always possible, but I wonder why since it is technically recursive so can explode on use. Especially if you use that on a record itself instead of pointer to record.
It keeps allocating memory in an infinite way.
Although here it uses a dynamic array of self and that is indeed a pointer.
If it were a static array, it explodes.
« Last Edit: July 03, 2024, 11:24:51 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Using the record type inside a self-declaration
« Reply #3 on: July 03, 2024, 11:27:52 am »
But FRecs there is a dynamic array, so all that's being allocated is a reference to the actual array storage on the heap.

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

ASerge

  • Hero Member
  • *****
  • Posts: 2494
Re: Using the record type inside a self-declaration
« Reply #4 on: July 03, 2024, 02:39:06 pm »
As far as I know this was syntactically always possible
In early versions of Delphi, where there were already dynamic arrays, this is explicitly a compilation error. I haven't checked in the latest versions.

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: Using the record type inside a self-declaration
« Reply #5 on: July 03, 2024, 03:01:45 pm »
Yes, same as FPC will throw an error. (Except D2, I believe, that simply crashes, can you test that 440bx?  :)  it is from memory and I need to install D2 from diskettes )
e.g.:
Code: Pascal  [Select][+][-]
  1. type
  2.   TRec = record
  3.     FRecs: array[0..3] of TRec;// this will cause an infinite amount of Trec allocations
  4.     FOther: Integer;
  5.   end;
is recursion and forbidden for the obvious reason.
Hence the dynamic array works, because it is a pointer type and managed.
« Last Edit: July 03, 2024, 03:11:54 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

440bx

  • Hero Member
  • *****
  • Posts: 6490
Re: Using the record type inside a self-declaration
« Reply #6 on: July 03, 2024, 06:19:06 pm »
Delphi 2 does not accept either definition.

The definition @Serge posted causes an "[ expected but 'OF' found" and the definition @Thaddy posted causes "Type 'TRec' is not yet completely defined' (which makes sense, that should never compile.)

Delphi 2 won't even accept this:
Code: Pascal  [Select][+][-]
  1. type
  2.   T = array of TREC;
  3.  

It looks like in Delphi 2, "array of something" is only allowed in parameter types not type definitions.


FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: Using the record type inside a self-declaration
« Reply #7 on: July 03, 2024, 08:11:24 pm »
Delphi 2 was before dynamic arrays. my question is if it crashes with the static array?
I have delphi 2, but need a reader for my disks.... Which I have, but my attick is even less organized than my brains...( some might say properly organized)
objects are fine constructs. You can even initialize them with constructors.

440bx

  • Hero Member
  • *****
  • Posts: 6490
Re: Using the record type inside a self-declaration
« Reply #8 on: July 03, 2024, 08:18:33 pm »
Delphi 2 was before dynamic arrays. my question is if it crashes with the static array?
I am not understanding what you are asking me to test.  Please provide a code snippet of what you'd like me to test.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: Using the record type inside a self-declaration
« Reply #9 on: July 03, 2024, 09:19:34 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   TRec = record
  3.     FRecs: array[0..3] of TRec;// this will cause an infinite amount of Trec allocations
  4.     FOther: Integer;
  5.   end;
that one. if you assign it to a variable. note this is a static array, not dynamic.
be careful, it leads to an out of memory error.. so plz be safe!
« Last Edit: July 03, 2024, 09:26:06 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

440bx

  • Hero Member
  • *****
  • Posts: 6490
Re: Using the record type inside a self-declaration
« Reply #10 on: July 03, 2024, 10:07:01 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   TRec = record
  3.     FRecs: array[0..3] of TRec;// this will cause an infinite amount of Trec allocations
  4.     FOther: Integer;
  5.   end;
that one. if you assign it to a variable. note this is a static array, not dynamic.
be careful, it leads to an out of memory error.. so plz be safe!
I tested that already and gave the result in the previous post.  The result is an error message from Delphi 2: "Type 'TRec' is not yet completely defined'

Which is the correct error message since the statement is semantically incorrect (it references itself.)

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2494
Re: Using the record type inside a self-declaration
« Reply #11 on: July 04, 2024, 03:50:31 am »
Hence the dynamic array works, because it is a pointer type and managed.
Thaddy, please check (the initial code) on the latest version of Delphi.

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: Using the record type inside a self-declaration
« Reply #12 on: July 04, 2024, 10:46:57 am »
I already checked that. Because the array is dynamic it works. Now change it to static and the compiler duely explains that Trec is not fully defined. Older Delphi's (D4) explode, D7 is the first I have that doesn't. I have most still installed as commandline compilers:3,4,2006,2007,2009, XE2. 3 didn't have dynamic arrays yet. I also have D2 and therefor also D1, and the kylixes,but they are not installed anywhere.
Note D4 on my machine has a little quirk, it uses the rtl and vcl from D5..
Don't worry, everything legal, I have licenses for all.
Side note: I also have a fpc2.6 with the Delphi5 rtl and vcl except for the system unit.
Note that the rtl and vcl are compiled with the gpl flag set, which is required. See the Kylix docs.
« Last Edit: July 04, 2024, 11:00:44 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Using the record type inside a self-declaration
« Reply #13 on: July 04, 2024, 10:55:42 am »
I already checked that. Because the array is dynamic it works. Now change it to static and the compiler duely explains that Trec is not fully defined. Older Delphi's (D4) explode, D7 is the first I have that doesn't.

Explosion is unambiguously wrong. However I think we have to take into account that even though declaring a dynamic array as a record field (in the same way that a non-short string may be declared) makes sense, at least some versions of the compilers might erroneously reject it as an error: even if able to generate correct code.

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: 19165
  • Glad to be alive.
Re: Using the record type inside a self-declaration
« Reply #14 on: July 04, 2024, 11:02:26 am »
The only one that I have and explodes is Delphi4. At least from D7 onwards the compilers throw errors, as does fpc. So it is basically a non-issue.
« Last Edit: July 04, 2024, 11:05:00 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

 

TinyPortal © 2005-2018