Forum > General

Nested declarations inside advanced record

(1/21) > >>

simone:
As is known, it is possible to nest the declarations of constants and types (as well as obviously variables) within a class. So we can write code like the following:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;{$mode objfpc}{$modeswitch advancedrecords} type   { MyRec }   TMyRec=class    private      const        C=5;      var        X, Y : integer;      type        TB=integer;    public      procedure Proc;  end; { MyRec } procedure TMyRec.Proc;begin end; beginend.
This syntax is expressly admittedfor by the diagram shown here:

https://www.freepascal.org/docs-html/current/ref/refse35.html#x70-940006.1

While testing a parser, I wondered if this was also possible for advanced records.

From the syntax diagrams shown here:

https://www.freepascal.org/docs-html/current/ref/refse61.html#x120-1440009.1

it would appear not.

To my surprise, I saw that the compiler accepts this syntax instead:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;{$mode objfpc}{$modeswitch advancedrecords} type   { MyRec }   TMyRec=record    private      const        C=5;      var        X, Y : integer;      type        TB=integer;    public      procedure Proc;  end; { MyRec } procedure TMyRec.Proc;begin end; beginend.
Therefore, should I assume that nested declarations of types and constants are also possible with advanced records?

Thanks in advance.



440bx:

--- Quote from: simone on January 08, 2025, 03:49:14 pm ---Therefore, should I assume that nested declarations of types and constants are also possible with advanced records?

Thanks in advance.

--- End quote ---
In theory it should because objects and classes are just extensions of advanced records.

Advanced records are the "seed" of OOP.  That's where it all starts.  Conceptually, Classes/Objects = Advanced records + inheritance + polymorphism.

simone:
I agree from a conceptual point of view, reasoning by analogy. My surprise comes from the fact that this is not documented.

Warfley:
Record and Object/Class parsing is kindof independent in the fpc source. (Advanced) Records are parsed in parse_record_members and objects are parsed in parse_object_members.
So this is not a situation that one works because the other one works and they just share the same code (which happens quite often with records and objects, e.g. parse_record_fields is used by both objects and records).

Advanced Records allows type, var, threadvar and const blocks, as well as property, (class) procedure/functions, constructor and destructor definitions.

I'm not sure if it's documented tho

simone:
Thanks Warfley.

Your clarification is especially important for what I'm doing. In the fcl-passrc parser, which I'm investigating for some personal projects, in line with what has been confirmed, in PasTree unit, TPasMembersType is the base type for TPasRecordType and TPasClassType.

Navigation

[0] Message Index

[#] Next page

Go to full version