Lazarus

Free Pascal => General => Topic started by: simone on January 08, 2025, 03:49:14 pm

Title: Nested declarations inside advanced record
Post by: simone on January 08, 2025, 03:49:14 pm
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  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}
  3. {$modeswitch advancedrecords}
  4.  
  5. type
  6.  
  7.   { MyRec }
  8.  
  9.   TMyRec=class
  10.     private
  11.       const
  12.         C=5;
  13.       var
  14.         X, Y : integer;
  15.       type
  16.         TB=integer;
  17.     public
  18.       procedure Proc;
  19.   end;
  20.  
  21. { MyRec }
  22.  
  23. procedure TMyRec.Proc;
  24. begin
  25.  
  26. end;
  27.  
  28. begin
  29. end.

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  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}
  3. {$modeswitch advancedrecords}
  4.  
  5. type
  6.  
  7.   { MyRec }
  8.  
  9.   TMyRec=record
  10.     private
  11.       const
  12.         C=5;
  13.       var
  14.         X, Y : integer;
  15.       type
  16.         TB=integer;
  17.     public
  18.       procedure Proc;
  19.   end;
  20.  
  21. { MyRec }
  22.  
  23. procedure TMyRec.Proc;
  24. begin
  25.  
  26. end;
  27.  
  28. begin
  29. end.

Therefore, should I assume that nested declarations of types and constants are also possible with advanced records?

Thanks in advance.



Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 08, 2025, 04:04:09 pm
Therefore, should I assume that nested declarations of types and constants are also possible with advanced records?

Thanks in advance.
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.

Title: Re: Nested declarations inside advanced record
Post by: simone on January 08, 2025, 04:19:04 pm
I agree from a conceptual point of view, reasoning by analogy. My surprise comes from the fact that this is not documented.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 08, 2025, 04:59:16 pm
Record and Object/Class parsing is kindof independent in the fpc source. (Advanced) Records are parsed in parse_record_members (https://gitlab.com/freepascal.org/fpc/source/-/blob/main/compiler/ptype.pas#L661) and objects are parsed in parse_object_members (https://gitlab.com/freepascal.org/fpc/source/-/blob/main/compiler/pdecobj.pas#L1073).
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
Title: Re: Nested declarations inside advanced record
Post by: simone on January 08, 2025, 05:26:24 pm
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.
Title: Re: Nested declarations inside advanced record
Post by: Thaddy on January 09, 2025, 12:02:06 pm
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.
Well, that is not true, although it might be conceptually true.
Originally there was no such thing as advanced records. That concept was a very much later addition to the language and even fairly recent. So your statement does not fit the time line.
History is records -> objects -> classes ->advanced records, in that order.
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 09, 2025, 03:17:34 pm
Well, that is not true, although it might be conceptually true.
There is no "might" about it. 

Apparently you're another one of those that thinks that what happens in FPC is how the world turns.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 09, 2025, 04:26:57 pm
There is no "might" about it. 
Except that the language that introduced OOP was SmallTalk, which didn't have records. Infact SmallTalk is not a classical procedural language to begin with (SmallTalk is Expression based not Statement based).

So it was first Smalltalk and then it was retrofitted to the structured type system of Algol style language like C and Pascal.

So sorry but the idea that there were first advanced records and then OOP developed from that is just completely ahistorical.

If you want to learn a bit about the original OOP, check out GNU SmallTalk (https://people.eecs.berkeley.edu/~fateman/264/papers/smalltalk-tutorial.html). You'll see that it's typesystem has absolutely no resemblence of Pascal (or Algols) structured record types
Title: Re: Nested declarations inside advanced record
Post by: simone on January 09, 2025, 04:43:33 pm
Except that the language that introduced OOP was SmallTalk

Historically this is not true: the first language that introduced OOP was Simula

(https://en.wikipedia.org/wiki/Object-oriented_programming)
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 09, 2025, 04:58:13 pm
Historically this is not true: the first language that introduced OOP was Simula

(https://en.wikipedia.org/wiki/Object-oriented_programming)
Right my mistake, SmallTalk was the first OOP language (i.e. including all the concepts like meta classes and stuff like that) , but the first language to introduce objects and the basic OOP concepts was Simula you are right.

Still Simula introduced already classes in full with most of the features we know today. "Advanced Records" where never a step in between
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 09, 2025, 05:00:15 pm
So sorry
You are rather sorry... you got that right.

Try to figure this out.. what critical characteristic of OOP do advanced records showcase ? 

reply when AND IF you figure out the answer.   Ask ChatGPT, it might be able to find the answer for you (and Thaddy.)

Title: Re: Nested declarations inside advanced record
Post by: Thaddy on January 09, 2025, 05:03:47 pm
Apparently you're another one of those that thinks that what happens in FPC is how the world turns.
Well, that is the order in which they were introduced in delphi/freepascal and TurboPascal does not even have advanced records, like it or not.
Provide evidence to  the contrary, you can't. Advanced records are a recent bolt-on. Noon of the major dialects know advanced records, just delphi and freepascal.
Title: Re: Nested declarations inside advanced record
Post by: alpine on January 09, 2025, 05:19:16 pm
Try to figure this out.. what critical characteristic of OOP do advanced records showcase ? 
Arghhhh, encapsulation, isn't it?

Stop nagging, Thaddy is right about that - most of us have experienced that transition while programming since the TP era.
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 09, 2025, 05:26:44 pm
There is one thing that has been clearly re-enforced by a select few forums participants and that is that, some people are surprisingly resistant to acquiring knowledge.  I guess it might have something to do with the fact that most of them seem to believe they know everything and were born that way (or got their "infinite" knowledge from reading FPC source code <chuckle>.)

The conclusion is: the results, which are usually very poor, are not worth my time.

I understand that you (Thaddy) like to argue anything, no matter how pointless, it makes your post count go up which seems to be important to you. ;)  Cheap thrills.




Try to figure this out.. what critical characteristic of OOP do advanced records showcase ? 
Arghhhh, encapsulation, isn't it?

Stop nagging, Thaddy is right about that - most of us have experienced that transition while programming since the TP era.
Yes, encapsulation.

Additionally, just because the grouping of variables (they become fields or some other fancy name) isn't called a "record" or "advanced record" in some other language, doesn't mean the language does not provide the mechanism.    It's a concept not a name.

Thaddy is _always_ right... haven't you figured that out yet ? ... and he is not the only one.

Title: Re: Nested declarations inside advanced record
Post by: Thaddy on January 09, 2025, 05:31:20 pm
Where is the proof? You are talking hot air.
Back to the topic:
All records can be nested, advanced or not. Simple answer and correct.
Since the inception of Pascal.

I ever claimed to be always right, but neither are you. I usually correct myself.
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 09, 2025, 05:38:46 pm
Since the inception of Pascal.
and then came the Big Bang because there was nothing before Pascal.

Title: Re: Nested declarations inside advanced record
Post by: simone on January 09, 2025, 06:07:05 pm
All records can be nested, advanced or not. Simple answer and correct.
Since the inception of Pascal.

Thaddy, I'm not sure about this. It seems to me that "conventional" records cannot have nested declarations like the ones in my second program in the first post. If you remove {$modeswitch advancedrecords} the program won't compile.
Title: Re: Nested declarations inside advanced record
Post by: Thaddy on January 09, 2025, 06:59:57 pm
Simple records can be members of higher records. That has always been the case.
You can generalize this to records can be members of records. Advanced or not.
If you give me a proper example where it fails in your view, I am happy to correct it.
Title: Re: Nested declarations inside advanced record
Post by: simone on January 09, 2025, 07:03:20 pm
Certainly, but the fact that a record can have fields that are also of the record type is a very different concept from that of nested declarations.

Try to compile this program:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}
  3.  
  4. type
  5.  
  6.   { MyRec }
  7.  
  8.   TMyRec=record
  9.     private
  10.       const
  11.         C=5;
  12.       var
  13.         X, Y : integer;
  14.       type
  15.         TB=integer;
  16.     public
  17.       procedure Proc;
  18.   end;
  19.  
  20. { MyRec }
  21.  
  22. procedure TMyRec.Proc;
  23. begin
  24.  
  25. end;
  26.  
  27. begin
  28. end.
  29.  
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 09, 2025, 08:21:52 pm
Try to figure this out.. what critical characteristic of OOP do advanced records showcase ? 

reply when AND IF you figure out the answer.   Ask ChatGPT, it might be able to find the answer for you (and Thaddy.)

The union of data and code. But whats your point? Note that the union of code and data is required for OOP but not sufficient. The same is true for closures yet closures are not considered OOP :)
Title: Re: Nested declarations inside advanced record
Post by: PascalDragon on January 09, 2025, 09:06:58 pm
Therefore, should I assume that nested declarations of types and constants are also possible with advanced records?

Thanks in advance.
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.

For a long time records in Pascal-based languages did not support type or constant declarations inside them (aside from anonymous types as part of field declarations). They were only ordered collections of fields and only Delphi 2007 (I think) introduced the concept of advanced records which allow to have type and constant declarations as well as methods declared in them.
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 09, 2025, 09:19:37 pm
<snip>   as well as methods declared in them.
THAT is the seed of OOP.  That's encapsulation and without it there is no OOP.

It looks like some people think that if it didn't start in the Pascal language then it must not exist.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 09, 2025, 09:27:40 pm
THAT is the seed of OOP.  That's encapsulation and without it there is no OOP.

It looks like some people think that if it didn't start in the Pascal language then it must not exist.

So closures are OOP? If so, congrats lisp was already OOP in the 50s
Title: Re: Nested declarations inside advanced record
Post by: PascalDragon on January 09, 2025, 09:36:50 pm
<snip>   as well as methods declared in them.
THAT is the seed of OOP.  That's encapsulation and without it there is no OOP.

If one thinks of records as the seed of OOP then one generally assumes records to be without methods, because records represent the state while the methods represent changes of the state and that plus inheritance is what OOP adds.

It looks like some people think that if it didn't start in the Pascal language then it must not exist.

Though Pascal was at least among the first to introduce the concept of records (https://en.wikipedia.org/wiki/Record_(computer_science)). ;)
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 09, 2025, 09:46:05 pm
It isn't about records... it is about the pairing of data and procedures in a dedicated structure which is what advanced records are and bring to the table.  That pairing is the door to OOP, without that, there is no OOP.

The name that pairing is given is irrelevant.  It's the concept's existence that matters, regardless of name.   Once you have that, you can extend the concept with inheritance and polymorphism.  Without encapsulation, you cannot have those and without those there is no OOP.

OMG !!!
Title: Re: Nested declarations inside advanced record
Post by: PascalDragon on January 09, 2025, 10:07:15 pm
It isn't about records... it is about the pairing of data and procedures in a dedicated structure which is what advanced records are and bring to the table.  That pairing is the door to OOP, without that, there is no OOP.

The name that pairing is given is irrelevant.  It's the concept's existence that matters, regardless of name.   Once you have that, you can extend the concept with inheritance and polymorphism.  Without encapsulation, you cannot have those and without those there is no OOP.

To be fair: that was not obvious from what you had written.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 09, 2025, 11:26:06 pm
OOP generally requires 4 properties:
1. Coupling of data (fields) and functionality (methods) into single entities (objects)
2. Types themselves are objects (of meta types) that have their own data and methods (static/class methods)
3. Inheritance and the ability to extend existing data types with additional data and/or functionality
4. Dynamic message dispatching within the object through runtime polymorphism (through virtual methods)

If any of these 4 is not satisfied, a language is generally not considered OOP. That's why for example Go is not considered an object oriented language, even though they cover 1. And 4.

Saying any one of them is the "seed" of OOP is not quite true as you need all 4 in combination for OOP. Many langues have similar properties, e.g. functional languages like Haskell or Lisp usually also combine data and code, and data types are usually also objects in the language. Infact Smalltalk was heavily influenced by Lisp. But because they don't provide the other properties it's not OOP
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 09, 2025, 11:33:31 pm
Saying any one of them is the "seed" of OOP is not quite true
I am not correcting you for your benefit but for others who may be reading this. 

Encapsulation is the _seed_ of OOP because THAT is what enables polymorphism and inheritance the way it's done in OOP.   The binding of code and data is what allows those extensions to exist.  Encapsulation is the distinguishing feature of what is called advanced records in Pascal that's what makes them the seed of OOP.

It's irrelevant that advanced records appeared in the language after objects and classes, the concept of encapsulation is  what matters.


Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 10, 2025, 12:08:30 am
Except.it doesn't, look at the Berkeley sockets api, it's a c API so not OOP, yet it also uses polymorphism based upon the passed data types of the sockaddr struct. If you pass a sockaddr_in to bind it will call the respective ipv4 functionality, if you pass a sockaddr_in6 to bind it will execute the respective code for ipv6.
Similarly if you call send on a UDP socket the UDP packet will be sent, while if you call it on a tcp socket you write data to a stream. Polymorphism over the data type without OOP

I know that you are quite familiar with the windows API, and the windows API does runtime polymorphism based on the data type passed all the time. So how can you claim it's only possible in OOP?

Infact polymorphism based on the data type passed, using common prefixes for data types is a well established practices long before OOP became a thing. Nothing in OOP is actually new and you can't boil OOP down to a single design decision, it's the combination of the 4 aspects above.
Title: Re: Nested declarations inside advanced record
Post by: Khrys on January 10, 2025, 07:44:56 am
So sorry
You are rather sorry... you got that right.

[...]

reply when AND IF you figure out the answer.   Ask ChatGPT, it might be able to find the answer for you (and Thaddy.)

There is one thing that has been clearly re-enforced by a select few forums participants and that is that, some people are surprisingly resistant to acquiring knowledge.  I guess it might have something to do with the fact that most of them seem to believe they know everything and were born that way (or got their "infinite" knowledge from reading FPC source code <chuckle>.)

The conclusion is: the results, which are usually very poor, are not worth my time.

[...]

Thaddy is _always_ right... haven't you figured that out yet ? ... and he is not the only one.

I find this kind of passive-aggressive condescension very irritating. Maybe you should take a look in the mirror before calling everyone who disagrees with you a stubborn know-it-all.
I do not mean to discredit your knowledge; your contributions to this forum contain very valuable information. But please consider turning down the smugness & sense of self-superiority a bit.
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 10, 2025, 08:46:52 am
I find this kind of passive-aggressive condescension very irritating.
I can understand that.  For the record, the individuals that get that from me more than earned it

Maybe you should take a look in the mirror before calling everyone who disagrees with you a stubborn know-it-all.
I take a look in the mirror every day.  It's not a matter of agreeing or disagreeing, it's a matter of simple decency and  honesty.  Warfley was given _multiple_ opportunities to explain his position but, since he is wrong and lacking both the honesty and the knowledge to acknowledge that, he gets and will keep getting the attitude from me that you find irritating.

What really irritates me are dishonesty, ignorance and continuous refusal to acknowledge being wrong when their being wrong is totally obvious.  I have little patience for that. period.  Being patient in the face of that kind of behavior just promotes more of it, not something that should be promoted.

All that said, for the record, I don't enjoy it but, I have no problem dishing out what someone worked hard to earn.


Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 10, 2025, 05:12:07 pm
What really irritates me are dishonesty, ignorance and continuous refusal to acknowledge being wrong when their being wrong is totally obvious.
You mean like, for example, if someone uses a well established term you haven't heard before, and the first reflex is to claim it's not used by anyone in the field, without doing even the slightest bit of checking which would have found that this term is used everywhere?
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 10, 2025, 06:08:23 pm
You mean like, for example, if someone uses a well established term you haven't heard before, and the first reflex is to claim it's not used by anyone in the field, without doing even the slightest bit of checking
First, I acknowledged the existence of the term, unlike you who has not acknowledged that the "duplicate identifier" message from the compiler stems from the fact that the parameter(s) and local variable(s) are in the same scope,
Second, as far as not doing "the slightest bit of checking", that is a malicious claim.  I have lost count of the number of compiler construction books I've read, not to mention compiler source code and I have not seen the term "shadowing" the way you used it.  That said, I openly conceded in the other thread and concede that the way you used it is correct.

which would have found that this term is used everywhere?
I have serious doubts about that because other than it being used by GCC (pointed out by PascalDragon), I haven't seen it used anywhere else but, I might have missed it being used here and there by other compilers, that's possible.

On the other hand, if it is used "everywhere" as you claim, show where FPC uses it. Everywhere should include FPC.  Just in case, used in a place that is visible to any user such as an error, warning or hint message.  You claim it's used everywhere, here is your chance to demonstrate your claim isn't simply more b.s. from you.

OTH,  let's have a look at your code again:
Code: Pascal  [Select][+][-]
  1.  procedure foo(x: Integer);
  2. var
  3.   x: Double;
  4. begin
  5. end;
Since you _dishonestly_ claim that the parameter "x" and the local variable "x" are not in the same scope, explain why a Pascal compiler emits a "duplicate identifier" error message.  As I stated a good number of times before: you got some explaining to do.

Now what ? are you going to post another pile of garbage about FPC source code that's neither here nor there ?  It's simple: explain why the compiler emits a "duplicate identifier" message, the reason is totally independent of how a compiler chooses to implement scope resolution.

I stand by every word I typed about you in my reply to @Khrys and your previous post only provides an additional example.

Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 10, 2025, 06:46:09 pm
First, I acknowledged the existence of the term, unlike you who has not acknowledged that the "duplicate identifier" message from the compiler stems from the fact that the parameter(s) and local variable(s) are in the same scope,
Second, as far as not doing "the slightest bit of checking", that is a malicious claim.  I have lost count of the number of compiler construction books I've read, not to mention compiler source code and I have not seen the term "shadowing" the way you used it.  That said, I openly conceded in the other thread and concede that the way you used it is correct.
Yeah you did concede, but only after denying that this term is widely used twice. That's the point, instead of going: "I don't know that term, I should search it up" you went straight to "I don't know that term therefore he must be wrong in using it"

Quote
I have serious doubts about that because other than it being used by GCC (pointed out by PascalDragon), I haven't seen it used anywhere else but, I might have missed it being used here and there by other compilers, that's possible.
Just timed: I just hit up Google with the query "shadowing site:clang.llvm.org" and found results in the clang documentation within less than a minute. I can do the same for "shadowing site:openjdk.org" or "shadowing site:python.org" just to check the tree most popular compilers/interpreters and I find results.

Again, it's not that you didn't know the term, and yeah you did concede, but the fact that you claimed first that this term isn't real and second that no big Compiler uses the term with such confidence, that's the problem I have with you.
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 10, 2025, 08:08:25 pm
Maybe you have a reading comprehension problem (in addition to others)... I asked for the term used in error, warning or hint messages.  I concede that the term exists and it is used, That said, I still have serious doubts it is as common as you imply it is because I don't see it used in compiler books, compiler source code and compiler messages.  It does appear in "Engineering a compiler" but not used the way you imply.   

Since you claimed the term is used "everywhere", I'd like to see where it's used in FPC.  Did you not comprehend the request ?  or did you skip that for convenience because your use of "everywhere" is not quite "accurate" (being nice here.)

Also, you apparently "forgot" to answer the question about your code:
Code: Pascal  [Select][+][-]
  1.  procedure foo(x: Integer);
  2. var
  3.   x: Double;
  4. begin
  5. end;
If the parameter and the local variable are not in the same scope, why does the compiler emit a "duplicate identifier" ?  You've been asked quite a bit more than twice and the correct (and honest) answer is  still yet to come.  I have little doubt that instead of answering the question you'll come back with something about "shadowing" being used "everywhere". 

Because of that, I take your non-answer reply as yet another example that my statements to Khris about you are fully accurate and fully deserved.

I'd be very surprised if your next post was not another pile of neither here nor there babble in yet another effort to sidestep the correct (and honest) answer.  but, surprise me.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 13, 2025, 12:49:06 am
Maybe you have a reading comprehension problem (in addition to others)... I asked for the term used in error, warning or hint messages.  I concede that the term exists and it is used, That said, I still have serious doubts it is as common as you imply it is because I don't see it used in compiler books, compiler source code and compiler messages.  It does appear in "Engineering a compiler" but not used the way you imply.
As I said, I've worked on multiple compilers and the term is beeing used everywhere. Like the internal clang documentation, the openjdk documentation or the python documentation.

Again it's a term that if you work on any active Compiler development, you will come across. Even when the word is not used in the Compiler sources themselves, you will see it used in issues or merge requests. While it is a term used by the official python documentation to describe certain behavior, you'll find most occurrences of the term in the python enhancement proposals, i.e. where individual authors or third parties bring in their proposals to the language.

Even if a Compiler itself is not using the term, if you work with compilers and interact with those communities, you will come across that term. So even if the authors of the python spec do not use it themselves, in order to write the spec they interact with proposals that use that term.

Again I have worked on actively developed compilers and programming languages, I also follow the development in some open source systems quite closely.
Maybe it's not that widely used in the academic literature (even though I learned the definition in my language engineering course at uni), but if you actually work in the field you will come across it

Also, you apparently "forgot" to answer the question about your code:
I have answered that question 4 times. Just to be very slowly for you again: The "Duplicate Identifier" message does not mean that two identifiers are in the same scope, it is thrown in multiple different circumstances, even in ones where the two symbols are clearly not in the same scope (as I have demonstrated). You're argument that because the "Duplicate Identifier" message is thrown, they are in the same scope, is just fallacious.

I did not "forgot" about it, I just did not want to drag that discussion to this thread also... There is nothing more to the discussion. I mean I rest asured that Niklaus Wirth in his book on Compiler Construction in section 12.4 has parameters and local declarations in different scopes (and yeah while it is for oberon, the visibility rules for variables and parameters in oberon are identical to pascal)... So if you only trust books (which again you shouldn't actual live compilers are much more authoratative on how compilers work than theoretical books), whose more trustworthy according to you Brinch Hansen or Niklaus Wirth?
Title: Re: Nested declarations inside advanced record
Post by: dsiders on January 13, 2025, 03:45:54 am
@Warfley, @440BX

Do you guys have to commandeer every thread for your little pissing contest?
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 13, 2025, 03:50:56 am
You're argument that because the "Duplicate Identifier" message is thrown, they are in the same scope, is just fallacious.
Really ?  explain why the compiler is emitting that message.   You're really good at claiming nonsense but, you still have some explaining to do.  I'll hint you the obvious: two identifiers cannot be duplicates of each other if they are in different scopes.

Here is the question _again_ (read slooooowly): if the parameters and the local variables are in different scopes explain why the compiler emits a duplicate identifier message.  What's the reason the compiler emits that message ? It's a simple question.

whose more trustworthy according to you Brinch Hansen or Niklaus Wirth?
Are you suggesting that Per Brinch Hansen got scoping wrong  ????.    Guess what.. what N. Wirth says in his book is the _same_ as what Per Brinch Hansen says in his.  The problem is: you don't understand what you read.  Now, I'd like you to explain how is it possible that PBH's compilers work if, as you claim, he doesn't get scoping right.  That should be interesting.

You really should read Per Brinch Hansen On Pascal Compilers.  IMO, it's the easiest/simplest introduction on compiler construction.  You should pay particular attention to the section about scopes I mentioned in a previous post.  After you read that book (multiple times would be good), follow that with Pemberton's comments on the P5 source code and you'll have a reasonably good foundation.

After you do that, you'll be able to answer the simple question you bend over backwards to avoid answering.




@Warfley, @440BX

Do you guys have to commandeer every thread for your little pissing contest?
No, AFAIC, only this one.  Note that I did not reply to his post in the other thread.


Title: Re: Nested declarations inside advanced record
Post by: dbannon on January 13, 2025, 04:35:43 am
@Warfley, @440BX

Do you guys have to commandeer every thread for your little pissing contest?

Well said Don. Honestly, the OP's question was answered in the first few posts, lets bring this to a close. Guys, you are obviously not going to convince the other one, so please stop trying !

Davo
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 13, 2025, 06:02:53 am
Guys, you are obviously not going to convince the other one, so please stop trying !
It's not a matter of convincing anyone.  My problem is that what he is stating is wrong and it is therefore misleading and mis-informing the forum users.    That's why I won't let go. 

The reason the compiler emits a "duplicate identifier" error message is because the parameters and the local variables are in the same scope.  His pretending that is not the case is dishonest and misleading.  It is completely unacceptable.


Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 13, 2025, 01:45:20 pm
I'll hint you the obvious: two identifiers cannot be duplicates of each other if they are in different scopes.
So just to understand you correctly, you say that if two identifiers are in different scopes, fpc would *never* throw a duplicate identifier message? If yes, you are simply wrong. If no, your argument doesnt make any sense.

Again I don't want to go into that further because your argument does not make any sense. I've demonstrated that the exact same error message is thrown for identifiers of different scopes (where you yourself said that the identifiers are in different scopes). So saying that because of that error message they must be in the same scope does not make any sense

Well said Don. Honestly, the OP's question was answered in the first few posts, lets bring this to a close. Guys, you are obviously not going to convince the other one, so please stop trying !
I tried to stop, and didn't answer in the last thread, but he brought it up again in this thread...
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 13, 2025, 06:24:19 pm
Warfley, you just can't stop posting that garbage of yours, totally unable to do the right thing.  Again, explain why the compiler emits a "duplicate identifier" error message.  If those two identifiers are not in the same scope then explain why the compiler emits that message ? 

And stop referring to FPC, it has nothing to do with FPC.  _ANY_ Pascal compiler would emit a duplicate identifier on that code.

Also, stop being so disgustingly dishonest.  This isn't about what I say, it's about language design.  See the reference to Per Brinch Hansen in the post: https://forum.lazarus.freepascal.org/index.php/topic,69755.msg542815.html#msg542815

Of course, he must be wrong because what he shows there is what you don't have the decency and honesty to admit, it's literally _drawn_ there for you.  Stop misleading readers,  answer the question (which is answered in PBH's book for you) or concede you are wrong.  Period.  You'll survive being honest, the majority of people "survive" it every day.
Title: Re: Nested declarations inside advanced record
Post by: jamie on January 13, 2025, 06:48:24 pm
@440 why do u have to be so cantankerous, go restock your supply of MIDOL.
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 13, 2025, 07:50:34 pm
@440 why do u have to be so cantankerous, go restock your supply of MIDOL.
I'm so sorry that expecting honesty from someone gets in your way too but, I have to say, it's no surprise it does.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 13, 2025, 10:46:05 pm
And stop referring to FPC, it has nothing to do with FPC.  _ANY_ Pascal compiler would emit a duplicate identifier on that code.
So logically speaking you say, For All Pascal Compilers follows that they will throw that error? So the falsification of that is: There exists at least one Pascal Compiler that doesn't throw that message?

So I just made a fork of the FPC: https://gitlab.com/Warfley/fpcsource/-/tree/argument?ref_type=heads
It allows the following:
Code: Pascal  [Select][+][-]
  1. procedure foo(x: integer);
  2. var x: Integer;
  3. begin
  4.  
  5. end;
But not this:
Code: Pascal  [Select][+][-]
  1. procedure foo;
  2. var x: Integer;
  3.   x: Double; // Duplicate identifier
  4. begin
  5.  
  6. end;
It is certainly exists and it is a pascal compiler, because it compiles any pascal program that FPC also compiles. I even tested it compiling both itself, the FCL and Lazarus all working flawlessly.
So now there exists at least one Pascal compiler that does not throw a duplicate identifier error there.


Note this change is just changing a single false to a true:
Code: Pascal  [Select][+][-]
  1.         { check also parasymtable, this needs to be done here because
  2.           of the special situation with the funcret sym that needs to be
  3.           hidden for tp and delphi modes }
  4.         hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  5.         if assigned(hsym) then
  6.           begin
  7.             { a local and the function can have the same
  8.               name in TP and Delphi, but RESULT not }
  9.             if (m_duplicate_names in current_settings.modeswitches) and
  10.                (sym.typ in [absolutevarsym,localvarsym]) and
  11.                (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  12.                not((m_result in current_settings.modeswitches) and
  13.                    (vo_is_result in tabstractvarsym(sym).varoptions)) then
  14.               Hidesym(sym)
  15.             else
  16.               DuplicateSym(hashedid,sym,hsym,true); // <-- this true was a false before
  17.             result:=true;
  18.             exit;
  19.           end;  
in symtable.pas. The reason this is so simple is because params and local variables are in different scopes, and there is just this one special case which does a cross scope check. If they would be in the same scope, it would be much more difficult to change that, as then I would have to deal with name collisions and stuff like that. But because they are in different scopes, I just need to change this single line and change the behavior from throwing an error to just shadowing the parameter.
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 14, 2025, 02:24:16 am
Wonderful... no only you can type a bunch of garbage in your posts, you can write garbage code too.  You really are impressively talented.

That said, you still haven't explained why Pascal compilers (that don't include code from you... LOL) emit a "duplicate identifier" error message.  The reason is genuinely trivial.

Aren't you tired of demonstrating you have no concept whatsoever of language design and compiler construction ?.  For the love of god, read Per Brinch Hansen On Pascal Compilers, it's an introductory book, really easy to read and he explains everything in detail.  He even draws how scopes operate and explains why.

Stop posting misleading garbage and start learning.
Title: Re: Nested declarations inside advanced record
Post by: Khrys on January 14, 2025, 08:24:26 am
Ok, so here's my theory to hopefully settle this once and for all:

Parameters and local variables are in different (albeit nested) scopes, and FPC explicitly disallows shadowing the former through a special check in  symtable.pas  (see Warfley's post).
This means that the "duplicate identifier" error message is a misnomer in this case; being an exception to the usual rules (shadowing generally being allowed), it should be marked as such.

Take the previous example:

Code: Pascal  [Select][+][-]
  1. procedure Foo(X: Integer);      // ---------+
  2. var                             // Foo:     |--------+
  3.   X: Integer;                   // Intf.    | Foo:   |
  4. begin                           // (Params) | Impl.  |
  5.   {Does not compile}            //          | (Body) |
  6. end;                            // ---------+--------+

Now let's change  X  to  Y  and add a nested function:

Code: Pascal  [Select][+][-]
  1. procedure Foo(X: Integer);      // ---------+
  2.   procedure Bar(W: Integer);    // Foo:     |----------+
  3.   var                           // Intf.    | Bar:     |--------+
  4.     Z: Double;                  // (Params) | Intf.    | Bar:   |
  5.   begin                         //          | (Params) | Impl.  |
  6.     {"X" -> Foo.X}              //          |          | (Body) |
  7.     {"Y" is not accessible}     //          |          |        |
  8.   end;                          //          |----------+--------+
  9. var                             //          |--------+
  10.   Y: String;                    //          | Foo:   |
  11. begin                           //          | Impl.  |
  12.   {Compiles just fine}          //          | (Body) |
  13. end;                            // ---------+--------+

The nested function can access the enclosing function's parameter X, but not its local variable Y, as it is declared separately.
It's also interesting to note that renaming  W  to  X  doesn't produce an error and simply shadows the enclosing function's parameter of the same name, while being in the same location that caused an error in the previous example; renaming  Z  to  X  also works flawlessly.
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 14, 2025, 09:05:28 am
Ok, so here's my theory to hopefully settle this once and for all:

Parameters and local variables are in different (albeit nested) scopes, and FPC explicitly disallows shadowing the former through a special check in  symtable.pas  (see Warfley's post).
This means that the "duplicate identifier" error message is a misnomer in this case; being an exception to the usual rules (shadowing generally being allowed), it should be marked as such.
This is truly unbelievable.

Parameters and local variables are in the same scope, this is obvious because if they weren't in the same scope then one of them would need to be prefixed with a scope identifier, i.e., <scope identifier>.<(parameter or local variable) identifier> depending on which one is incorrectly considered to be outside the function/procedure block.

The "duplicate identifier" is in no way a misnomer.  That's completely absurd. 

However FPC implements its scope resolution is as irrelevant to scope resolution as the color of the Pope's underwear on Tuesdays. 

Does anyone really think that Delphi, Gnu Pascal, UCSD Pascal, etc, etc, use the same algorithms and code as FPC ? really ???? Stop inhaling red paint for breakfast, it's not good for you.   

Reading FPC's source code doesn't teach basic language design and implementation principles.  FPC doesn't even show which production its parser code implements. 

Read PBH's books, Pemberton's comments on P4 (or P5, don't remember which one it addresses right now) and, if you want to see the source of a well written compiler, read PBH's SuperPascal's source code.   

Title: Re: Nested declarations inside advanced record
Post by: Khrys on January 14, 2025, 10:47:04 am
Parameters and local variables are in the same scope [...]

Why can  Bar  access  Foo's parameter  X  but not its local variable  Y  then?
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 14, 2025, 12:27:24 pm
Parameters and local variables are in the same scope [...]

Why can  Bar  access  Foo's parameter  X  but not its local variable  Y  then?
You know the answer to that: because Y is defined _after_ procedure Bar. As I have no doubt you know, Pascal requires all identifiers to be defined before they can be accessed.

Here is your code slightly modified to illustrate some cases (Note that "Y" is still in Foo but it's declared sooner):
Code: Pascal  [Select][+][-]
  1.  
  2. procedure Foo(X: Integer);      // ---------+
  3. var
  4.   Y : String;
  5.  
  6.   procedure Bar(W: Integer);    // Foo:     |----------+
  7.   var                           // Intf.    | Bar:     |--------+
  8.     Z : Double;                 // (Params) | Intf.    | Bar:   |
  9.  
  10.  
  11.     X : byte;        { masks (or shadows if you prefer) the parameter X }
  12.  
  13.   begin                         //          | (Params) | Impl.  |
  14.     {"X" -> Foo.X}              //          |          | (Body) |
  15.     {"Y" is not accessible}     //          |          |        |
  16.  
  17.  
  18.     X := 355;         { will emit a warning if X (in bar) exists, won't otherwise }
  19.  
  20.                       { --------------- }
  21.     Y := '';          { access Y in Bar }
  22.                       { --------------- }
  23.  
  24.   end;                          //          |----------+--------+
  25. //var                             //          |--------+
  26. //  Y: String;                    //          | Foo:   |
  27. begin                           //          | Impl.  |
  28.   {Compiles just fine}          //          | (Body) |
  29. end;                            // ---------+--------+
  30.  
Now Bar can access Y without any problem.

In the above, the local variable "X" in Bar masks (or shadows if you prefer) Foo's parameter "X".  Here is a little experiment I'd like you to do: compile the above code and you'll get a warning stating that the value of "X" is out of range (355 is larger than what fits in a byte), that proves that the compiler is using the definition "X : byte".  After that, comment out the definition "X : byte" and recompile.  Note that the warning will be gone proving that it is the parameter "X" that is being accessed. 

The local "X : byte" and parameter "X : integer" are most definitely NOT in the same scope but, "W" and "Z" are most definitely in the _same_ scope.  the same way that the parameter "X" and local variable "Y" are also in the same scope (just  in case, a different scope than the one that contains "W" and "Z".)

Scoping is very nicely explained in PBH's book as well as a simple yet effective and fully functional implementation.  I've recommended PBH's book for years precisely because it focuses on the concepts, gives clear explanations and fairly simple implementations that are easy to understand and they work.   

To be fair, he did cut a few corners here and there but not in concepts.  That's fair because he clearly states in the introduction, it's a book meant as a basic introduction to language grammars and compiler construction.   It's an introductory course.

Also and likely very important in this discussion, in Pascal the scope resolution implementation always accounts for nested scopes but that doesn't mean that in the code:
Code: Pascal  [Select][+][-]
  1.  procedure foo(x: Integer);
  2. var
  3.   x: Double;
  4. begin
  5. end;
that the parameter "x" and local variable "x" are in different scopes, they are not, they are in the same scope.  However, there is no doubt in my mind that the code that handles that simple case is the very same code that handles nested scopes since "procedure foo" is nested in the program, therefore defines a nested scope.

IOW, it is completely incorrect to conclude that because the code can handle nested scopes that somehow parameter "x" and local variable "x" are different scopes.  Again: parameter "x" and local variable "x" are in the same scope. which is why the compiler emits a "duplicate identifier" message.


Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 14, 2025, 12:53:28 pm
That said, you still haven't explained why Pascal compilers (that don't include code from you... LOL) emit a "duplicate identifier" error message.  The reason is genuinely trivial.
Interesting, so do you say that the FPC is a pascal compiler only if it doesn't contain code by me? If so, I'm sorry to tell you but the FPC already contains code by me since 2023 :)
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 14, 2025, 01:02:30 pm
Interesting, so do you say that the FPC is a pascal compiler only if it doesn't contain code by me? If so, I'm sorry to tell you but the FPC already contains code by me since 2023 :)
No, what I am saying is that a Pascal compiler is significantly more likely to function correctly (and sensibly) if it doesn't contain code from you. 


Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 14, 2025, 01:13:18 pm
However FPC implements its scope resolution is as irrelevant to scope resolution as the color of the Pope's underwear on Tuesdays. 

Does anyone really think that Delphi, Gnu Pascal, UCSD Pascal, etc, etc, use the same algorithms and code as FPC ? really ???? Stop inhaling red paint for breakfast, it's not good for you.   

Again your problem is you lack a consistent definition of what a scope is. What is a definition that encompasses what you are talking about?

Like let's take the description of scope from Niklaus Wirth in his book Compiler Construction:
Quote
The symbol table consists of lists of elements, one for each scope (see Section 8.2). The
elements consist of the name (identifier) and a reference to the identified object.
Code: Pascal  [Select][+][-]
  1. Ident = POINTER TO IdentDesc;
  2. IdentDesc = RECORD name: ARRAY 32 OF CHAR;
  3. obj: Object; next: Ident
  4. END ;
  5. Scope = POINTER TO ScopeDesc;
  6. ScopeDesc = RECORD first: Ident; dsc: Scope; END;
Here the definition of a scope is completely practical, a scope is just an entry in the stack of symbol lists that form the symbol table.
But according to that, in FPC, which is it's own pascal dialect (there is no "true" pascal, each compiler provides their own dialect with their own rules), parameters and variables are in different scopes.

But you could also go with a more formal definition, like is alluded to in the book by brinch hansen, that a scope is a set of rules according to which symbols are resolved. This is a very useful definition for formal analysis, but then any new declaration creates a new scope, because it is only referencable after it's declaration.

This is also a correct way to define scopes, it's not useful for implementing a compiler, but if you do formal analysis of a program (like I have done in my research job in the past), this is how you will mathematically model the scopes.

So there is a practical way to define scopes, as the structuring of the symbol table, and a mathematical definition of scopes used in formal analysis. I have seen both in practice, I have used both for real world applications, they are both equally valid for different purposes. But crucially according to both of these definitions you are wrong
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 14, 2025, 02:04:42 pm
Again your problem is you lack a consistent definition of what a scope is. What is a definition that encompasses what you are talking about?
No, that is your problem.  I _showed_ you what a scope is and encompasses by referring to PBH's chapter on scoping where he even draws the scopes and what they contain. 

The concept of scope is totally independent of how it is implemented and what you showed there is a very limited definition of a possible implementation. 

Of course, you'll keep posting trash because you don't want to concede that for two identifiers to be considered duplicates they must reside in the same scope.  That fact, is totally independent of how scope resolution is implemented.

In the following code:
Code: Pascal  [Select][+][-]
  1.  procedure foo(x: Integer);
  2. var
  3.   x: Double;
  4. begin
  5. end;
You are yet to explain why the compiler emits a duplicate identifier message.  It's really simple and by now I've lost count of the number of times I've explained why to you (and, unfortunately also had to explain it to a forum member you've confused with your garbage.)


Title: Re: Nested declarations inside advanced record
Post by: Khrys on January 14, 2025, 02:08:19 pm
Here is your code slightly modified [...]

Irrelevant. This exact code is a counterexample to your claim, using the definition of a scope from Brinch Hansen on Pascal compilers (http://pascal.hansotten.com/uploads/pbh/brinch%20hansen%20on%20pascal%20compilers%20OCR.pdf),  6.2 SCOPE RULES  (page 107 of the .pdf):

Quote from: Brinch Hansen on Pascal Compilers
We must also specify in which part of a block an object can be referenced. This part of the block is called the scope of the object. An object is said to be known in its scope.

So the scope of an object is the part of the block in which it can be referenced.

Parameter  X  can be referenced inside  Bar.
Local variable  Y  can not be referenced inside  Bar.
Therefore the scope of  X  differs from the scope of  Y.

(I'd even go so far as to say that two different objects can never share the same scope, as that would imply that they are the same object.)
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 14, 2025, 02:20:50 pm
Local variable  Y  can not be referenced inside  Bar.
That is so ridiculous, it's unbelievable.

In your example "Y" couldn't be referenced because it had not been defined.  It's not a matter of scopes, it's a matter of definition sequence.  Unbelievable!!!

The sad part is that you've got all that Warfley trash in your head.    He is beyond help and it looks like you are in that boat too.  Oh well.

The two of you should get a room, make each happy... LOL

Title: Re: Nested declarations inside advanced record
Post by: Khrys on January 14, 2025, 03:00:49 pm
In your example "Y" couldn't be referenced because it had not been defined.  It's not a matter of scopes, it's a matter of definition sequence.  Unbelievable!!!

May I remind you that the question was whether parameters and locals share the same scope?

It doesn't matter how or why  Y  is unable to be referenced. I took a formal definition of what a scope is (from a source you gave) and showed via counterexample that parameters and locals do not in fact share the same scope, all in 4 lines of simple English. If that's too much for you to comprehend, then I'm afraid I can't help you.

I'm leaving this discussion to touch some grass (https://www.merriam-webster.com/dictionary/touch%20grass).
Title: Re: Nested declarations inside advanced record
Post by: ASerge on January 14, 2025, 06:17:23 pm
I agree with @440bx that the scope of the function parameters is the same as the scope of the local variables of this function. And this is not only in FPC, but also in other languages.
In the implementation of the FPC compiler, symbol tables for local variables and symbol tables for parameters were separated only for check duplication on the names of properties and method parameters in the class. Formally, they are from different scopes, but the FPC compiler has implemented this additional protection.
And the fact that @Warfley was able to hack the implementation of the compiler to cancel the correct check does not change the situation.
Title: Re: Nested declarations inside advanced record
Post by: Thaddy on January 14, 2025, 07:02:29 pm
It may be scope on the same level, but it is not the same scope.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 14, 2025, 07:38:44 pm
In the implementation of the FPC compiler, symbol tables for local variables and symbol tables for parameters were separated only for check duplication on the names of properties and method parameters in the class.
No parameters are in a different symtable for many more reasons, most importantly to match forward declarations. When the compiler finds a local implementation of a procedure, it checks if it has already found a forward declaration that contains the same symbols, as in ObjFPC not just the signature must be identical but the symbols must be identical:
Code: Pascal  [Select][+][-]
  1. procedure Foo(i: Integer); forward;
  2. procedure Foo(j: Integer);
  3. begin
  4. end;
This is not allowed in Pascal, unlike in other languages like in C.

Also parameters are visible to the callsite of a function:
Code: Pascal  [Select][+][-]
  1. procedure Foo(var i: Integer);
  2. begin
  3. end;
  4.  
  5. begin
  6.   Foo(42);
  7. end.
This throws an error because 42 is not a variable, how does the compiler know that? By looking up the parameter of the function call. Therefore the parameter must be visible at the callsite, note that the compiler does not have access to any information of local variables at the callsite.

They have different visibilities, different naming rules, different restrictions, and are technically in a different symtable. How does any of that apply to local variables?
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 14, 2025, 09:05:50 pm
Thank you @ASerge, it's encouraging to see there are people who understand scope resolution.

Anyway, I am done attempting to explain a rather basic concept to people who have demonstrated being unable and unwilling to learn.

For those who want to understand compiler concepts, read PBH's books and read the source code of his SuperPascal compiler, his code is easy to understand.  PBH explains things very clearly.  A good follow up are the P4 and P5 implementations, particularly accompanied by Pemberton's comments about them.  He points out a lot of small but critical details.

Another interesting implementation is Vasiliy Tereshkov' xdpw (search this forum to find it) which is also self hosting and can be compiled with FPC.  That compiler has a very easy to understand implementation of scopes and the code makes it crystal clear that parameters and locals are in the same scope.  Read Parser.pas, procedure DeclareIdent.


@Warfley, @Khrys, @Thaddy, go ahead, peddle that ridiculous and wrong garbage of yours.  I won't correct you anymore,  mislead people with your invulnerable ignorance.  This is as fruitful as attempting to educate a rock.

Done!

Title: Re: Nested declarations inside advanced record
Post by: Joanna on January 15, 2025, 12:24:44 am
The discussion about scopes was interesting, I’m not enough of an expert to have an opinion but it seems like parameters that originate elsewhere and local variables which only exist inside of a procedure are not the same thing even though their names can clash.

Back to an earlier topic on the thread.. I’m curious why advanced records were needed when classes and objects already existed?

Was it because Delphi was doing it or ?
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 15, 2025, 12:28:58 am
Back to an earlier topic on the thread.. I’m curious why advanced records were needed when classes and objects already existed?

Was it because Delphi was doing it or ?
No. Simply because inheritance and polymorphism aren't always needed and, when they aren't, simple (un-extended) encapsulation is more efficient.
Title: Re: Nested declarations inside advanced record
Post by: Joanna on January 15, 2025, 12:48:23 am
That’s a strange way of doing things, wouldn’t it be easier just not to use those features?

I’ve also heard that advanced records can initialize themselves automatically. Maybe that’s useful
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 15, 2025, 12:58:14 am
Not considering managed records for a second, plain old advanced records are just syntactic sugar.
Code: Pascal  [Select][+][-]
  1. TMyRec = record
  2.   x: Integer;
  3.   procedure PrintX;
  4. end;
  5. // Is the same as
  6. TMyRec = record
  7.   x: Integer;
  8. end;
  9. procedure MyRecordPrintX(var self: TMyRec);

So "needed" is quite a strong word, they don't enable something that was previously not possible. But they are still useful for a few reasons:

1. Auto complete and code locality, when writing rec. And hitting Ctrl+space you see all the methods of that record. With the subject.verb(object) Syntax it's very easy to discover the possibilities of how the record can be used by just browsing the methods that come up in auto complete. Or alternatively because all the methods need to be in the record definition it forces the programmer to have all the functionality related to a data type in one place.
It just organizes the code easily

2.  Visibility scopes: by now having the ability to have private fields, you can make a Differentiation between internal state and what's visible to the users. While not necessarily that important for your own project, it's great for writing libraries where the user of the library does not need to concern themselves with the inner workings of it

3. Similarity with objects and classes: not just for familiarity of the programmer, but also this enables to use the same code for objects, classes and records. If you write a generic function, that requires a ToString method, you don't care if it's a class, an object or a record that's been used as generic specialization. A special case for this is enumerators, which must provide a MoveNext function and a Current property, which means if advanced records wouldn't be a thing you couldn't have records as enumerators.

4. Generic records and operator overloading: without advanced records where you define operators as part of the record you can't overload operators for generic types

Then with managed records you also gain additional functionality in form of automatically called initializer and finalizer code, which allows things that have previously not been possible at all, and are not possible with either classes or objects
Title: Re: Nested declarations inside advanced record
Post by: ASerge on January 15, 2025, 02:19:01 am
It may be scope on the same level, but it is not the same scope.
What the "level"?
From FPC doc Scope (https://www.freepascal.org/docs-html/current/ref/refse116.html#x237-26100016.6): Identifiers are valid from the point of their declaration until the end of the block in which the declaration occurred. The range where the identifier is known is the scope of the identifier.
The parameters are declared inside the function and are valid until its end.
It's so simple and obvious, and it's also well-known. It seems to me that @Warfley and @Thaddy are just trolling out of boredom.
Title: Re: Nested declarations inside advanced record
Post by: Joanna on January 15, 2025, 02:46:20 am
It seems that the term scope is ambiguous.
The local variable x is using block scope.
The procedure foo(x: Integer); is accessible from outside of the procedure possibly with unit scope? This would give it priority wouldn’t it ? being that it was declared first.
Title: Re: Nested declarations inside advanced record
Post by: Thaddy on January 15, 2025, 06:00:28 am
Scope is nothing more than an isolation level. ASerge at al. refuse to get that.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 15, 2025, 06:01:46 pm
What the "level"?
From FPC doc Scope (https://www.freepascal.org/docs-html/current/ref/refse116.html#x237-26100016.6): Identifiers are valid from the point of their declaration until the end of the block in which the declaration occurred. The range where the identifier is known is the scope of the identifier.
The parameters are declared inside the function and are valid until its end.
It's so simple and obvious, and it's also well-known. It seems to me that @Warfley and @Thaddy are just trolling out of boredom.
This is something I have also brought up in the discussion multiple times, especially in my last post. On a theoretical level specifically for program analysis, every identifier spans their own scope. This is how you model scopes in analysis and formalizing the code. It's a valid definition that's used in practice.

But there's also a technical aspect of scope, for which this definition is quite cumbersome. On a technical level a scope is spanned by a symbol table on the symbol table stack. It describes an object in the implementation that's responsible for resolving names. That's what for example the clang::scope (https://clang.llvm.org/doxygen/classclang_1_1Scope.html) class is doing, or in FPC terms the tsymtable class in symtable.pas. This is also a valid definition and is used in practice.

Depending on the context you use either one of these ways to define scope. Note other languages like JavaScript do not have the first kind of scope, as you can use variables before they have been declared:
Code: Java  [Select][+][-]
  1. var i = j; // works because j is defined below
  2. var j = k; // error, unknown identifier k
(At least original JavaScript, since some time now JavaScript has the let keyword which is only available after it's been declared)

The original discussion was about the implementation of the FPC, so I was talking about the technical definition, but I did mention that the theoretical definition of a scope also exists and is fully valid). But it doesn't matter anyway which of the two definitions of scope you use, neither the theoretical nor the technical definition make parameters and local variables have the same scope. There is no definition of scope where parameters and local variables have the same scope
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 15, 2025, 07:48:05 pm
There is no definition of scope where parameters and local variables have the same scope
The only thing greater than your incompetence is your dishonesty.  PBH _draws_ the scopes in his book clearly showing the parameters and the local variables residing in the same scope.

But, of course, _you_ know more about language grammars and compiler construction than PBH.  (of course you do because you've read FPC's source code, that must make all the difference.)

In his paper, Compiler Construction, The Art of Niklaus Wirth, Hanspeter Mössenböck from the University of Linz states:
Quote
A new scope is opened at the top of the stack whenever the parser encounters a procedure, and it is closed when the parser has finished with this procedure.
Of course, you'll babble some garbage to argue the meaning of that.  Never mind that the guy worked with N. Wirth (obviously you know better than him  too, that should go without saying)   One scope per procedure clearly implies the procedure's parameters and its locals are in the same scope (not to mention other declarations that can reside in a procedure.)

It's going to be interesting to see the trash you come up with to dispute his statement.  I got me some popcorn.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 15, 2025, 08:11:30 pm
PBH _draws_ the scopes in his book clearly showing the parameters and the local variables residing in the same scope.
He draws the scopes how it is convinient for the reference implementation he builds throughout the book implements them, but where is his that compiler and how many people are it today? FPC draws scopes how it is convinient for it's implementation, you know for a compiler that finds real world usage.
In both cases the scopes are based on whats convinient for the implementation. I personally go with the implementation that is actually used in the real world :)
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 15, 2025, 09:02:33 pm
In both cases the scopes are based on whats convinient for the implementation.
and that is a lie.  The implementation is usually driven by convenience BUT, the concept is NOT a matter of convenience.

As the drawing in PBH's book clearly shows, the parameters and the local variables are in the same scope.  A drawing is NOT an implementation and, in this case, that is rather inconvenient for you. <chuckle>

It would be nice, for you and the people you are misleading, if you managed to understand the concepts the code you read implements.  If you understood that, you'd also understand why the compiler emits a "duplicate identifier" message on that code you posted.

Unfortunately, it looks like that's way too much to expect from you.
Title: Re: Nested declarations inside advanced record
Post by: lainz on January 15, 2025, 09:07:35 pm
Scope is nothing more than an isolation level. ASerge at al. refuse to get that.

Nice avatar Thaddy, the cat from the lamp =)
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 15, 2025, 09:11:20 pm
Scope is nothing more than an isolation level. ASerge at al. refuse to get that.

Nice avatar Thaddy, the cat from the lamp =)
I believe that is from forum user @Fibonacci. I believe it was AI generated.
Title: Re: Nested declarations inside advanced record
Post by: lainz on January 15, 2025, 09:46:09 pm
Scope is nothing more than an isolation level. ASerge at al. refuse to get that.

Nice avatar Thaddy, the cat from the lamp =)
I believe that is from forum user @Fibonacci. I believe it was AI generated.

Not bad for AI... but it draws hands with incorrect number of fingers  :)

Edit: my avatar is the "Space Dog" and is 100% drawn by humans, me.  8-)
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 15, 2025, 11:00:55 pm
and that is a lie.  The implementation is usually driven by convenience BUT, the concept is NOT a matter of convenience.
You are getting there :) Like the concept of the scope of a variable is the block where it is visible, so as was shown multiple times already, a variable can only be referenced after it has been declared, therefore it's scope starts below it's definition. Thats literally the concept described in the book:
Quote
We must also specify in which part of a block an object can be refer-
enced, This part of the block is called the scope of the object. An object is said to be known in its scope.
Roughly speaking, an object defined in a block is known from its definition to the end of the block.
So the scope of an object starts at it's definition and ends at the end of the block the definition occurs in. This is the concept that PBH describes. But then for the implementation purposes PBH creates a symtable stack of lists, which he visualizes. So the drawing you referring to is already a deviation from the concept PBH has introduced just 4 pages earlier.

As the drawing in PBH's book clearly shows, the parameters and the local variables are in the same scope.  A drawing is NOT an implementation and, in this case, that is rather inconvenient for you. <chuckle>
That is just an abstraction thats convinient for the reference compiler he built in his book. As it deviates from his own definition of a scope above.

The thing is, his reference compiler is just a very simple academic example. Compilers in the real world get messy. Want an example? The compiler in PBHs book is a strict LL(1) recursive descent parser, which is great as an didactic example. If you look at any current Pascal compiler, e.g. Delphi or FPC, they are not strictly LL(1). There are language constructs that require some form of internal backtracking.

You are referencing a single didactic decision PBH made to visualize a very simple compiler, oversimplified infact, to make the introduction into compilers easy. Real compilers are much more complex and require different and more complex abstractions. In FPC the technical scope is much more nuonced, because it makes technical sense for the features of FPC. Again... no one is using that compiler PBH wrote in the real world, because it's just a demo, and you can't take the abstractions chosen for the purposes of explaining compiler construction to absolute beginners, as universal concepts all compilers must adhere to
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 15, 2025, 11:58:39 pm
You are referencing a single didactic decision PBH made to visualize a very simple compiler, oversimplified infact, to make the introduction into compilers easy.
Whether a compiler is simple or not, the concepts don't change.  In Pascal, parameters are in the same scope as local variables.  Apparently this simple concept is too complex for you to comprehend.  Your attempt to devalue PBH's work clearly reflects the kind of person you are.

N. Wirth and Hanspeter Mössenböck must also be wrong because as stated in Compiler Construction - The Art of Niklaus Wirth:
Quote
A new scope is opened at the top of the stack whenever the parser encounters a procedure, and it is closed when the parser has finished with this procedure.
Maybe N. Wirth and Hanspeter Mössenböck didn't work on compilers that are complex enough for you.  That must be the reason... no doubt.

Stop misleading people with your trash.  You have no grasp of even the simplest compiler concepts. 

Title: Re: Nested declarations inside advanced record
Post by: lainz on January 16, 2025, 02:33:54 am
I don't know which one is right...  :o
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 16, 2025, 04:26:28 am
I don't know which one is right...  :o
I assure you, Per Brinch Hansen is right as are N. Wirth and Hanspeter Mössenböck.  This should not be a surprise to anyone. 

Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 16, 2025, 05:52:37 pm
Brinch Hansen says:
Quote
An object is said to be known in its scope. Roughly speaking, an object defined in a block is known from its definition to the end of the block.
So he clearly writes black and white an objects scope is from its definition to the end of the block. This means any definition creates its own scope, and therefore no two objects have the same scope.
So if you think Brinch Hansen is correct, it means parameters and variables are in different scopes (infact any two local variables or parameters have a different scope from each other, because they are defined at different points)

See this:
Code: Pascal  [Select][+][-]
  1. procedure Foo(x: Integer = SizeOf(y)); // error unknown identifier y
  2. var
  3.   y: Integer = sizeOf(x); // works
  4. begin
  5. End.
Y can reference x but not the other way around, because they have different scopes
Title: Re: Nested declarations inside advanced record
Post by: Thaddy on January 16, 2025, 06:28:20 pm
Frederic, you are right, but don't be too pedantic. :P
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 16, 2025, 07:21:21 pm
Brinch Hansen says:
Quote
An object is said to be known in its scope. Roughly speaking, an object defined in a block is known from its definition to the end of the block.
So he clearly writes black and white an objects scope is from its definition to the end of the block. This means any definition creates its own scope, and therefore no two objects have the same scope.
PBH didn't draw a separate scope for every variable and parameter in the section about scoping.  Go look at the drawing again. 

Your problem is that you want to equate visibility with scope.  As far as visibility, an object (_not_ in the sense of oop) is visible from the point where it is defined and not before (and even that is disputed/questioned, there are those who make a good case that the object is visible not from where it's defined but from the beginning of the scope to the end regardless of where in the scope the object is defined.)

Regardless of the view on that matter, parameters and locals are in the same scope which is why your code causes the compiler to emit a "duplicate identifier" message.  You can keep posting garbage but, the fact remains, you cannot explain _why_ the code you posted results in a "duplicate identifier" message, that conclusively proves all your babble is pure _garbage_.


Title: Re: Nested declarations inside advanced record
Post by: ASerge on January 16, 2025, 07:59:57 pm
Code: Pascal  [Select][+][-]
  1. procedure Foo(x: Integer = SizeOf(y)); // error unknown identifier y
  2. var
  3.   y: Integer = sizeOf(x); // works
  4. begin
  5. End.
Y can reference x but not the other way around, because they have different scopes
An unfortunate example, because y defined AFTER x. The same as
Code: Pascal  [Select][+][-]
  1. procedure Foo;
  2. var
  3.   x: Integer = SizeOf(y); // Error: Identifier not found "y"
  4.   y: Integer = sizeOf(x); // works
  5. begin
  6. end;
Title: Re: Nested declarations inside advanced record
Post by: Thaddy on January 16, 2025, 08:03:43 pm
@440bx,
https://www.youtube.com/watch?v=b19PcuJsQbA

This deserves a big sigh for ignorance.

Maybe part 2 will calm you down.
Title: Re: Nested declarations inside advanced record
Post by: Zoran on January 16, 2025, 08:07:05 pm
I don't believe you have been spending so much energy on something totally unimportant in practice.
Who cares whether parameters and local variables are technically in the same scope?

Of course it is important that programmer understand scope rules, but that actually includes being aware that for all practical purposes parameters and local variables can be seen as being in the same scope.

Your discussion has not been helpful at all. If you want to leave something useful in this forum, it is not about who of you is right, the only thing that really matters is that if they are in different scopes, it is not important.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 16, 2025, 08:38:46 pm
PBH didn't draw a separate scope for every variable and parameter in the section about scoping.  Go look at the drawing again.
Yes the drawings are illustrations of the technical abstractions of this concept he used to implement his compiler. Any technical abstraction will somewhat diverge from the theoretical concept. This is what I'm telling you for the past probably 15 posts now.
PBH chose a certain abstraction which is useful for the compiler he is building for didactic purposes in that book. But a different setting will have different requirements for the abstraction, which is why FPC draws it's scopes differently.

You can either go by the theoretical definition according to which every declaration has it's own scope, or you go by the practical abstraction. If you go by the practical abstraction you must accept that it is going to be different from one compiler to the next. For the Pascal Dialect FPC supports, the same abstraction PBH uses does not make sense, so it puts Parameters and Variables into different scopes.

Note that this is not just FPC, clang does the exact same for C++, here local variables are in a different scope than parameters, even though the theoretical definition of a scope (from the declaration to the end of the block) is exactly the same.

I have worked with the sources of multiple compilers. I do not know of a single real world compiler that puts local variables and parameters into the same scope. Because aside from some reference compilers that are not intended to be used by anyone productively, it does not make sense to do so.

An unfortunate example, because y defined AFTER x. The same as
Code: Pascal  [Select][+][-]
  1. procedure Foo;
  2. var
  3.   x: Integer = SizeOf(y); // Error: Identifier not found "y"
  4.   y: Integer = sizeOf(x); // works
  5. begin
  6. end;
The example shows exactly what I am describing. On a theoretical level, no two (separate) declarations can have the same scope.

As I said multiple times. There is the theoretical way of looking at it (each declaration has it's own scope from the declaration to the end), and a technical scope, which is how it is implemented.
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 16, 2025, 09:20:54 pm
The scope of parameters and local variables starts at the beginning of the procedure block and ends at the end of the procedure block.  Where their visibility starts is not the same.

How a compiler implements scopes is completely irrelevant.

Make stuff up all you want but, the fact remains, in the code you showed the compiler declares a "duplicate identifier" because the parameters and the local variables are in the same scope.  This has absolutely nothing to do with how it is implemented.   

From one Pascal compiler to another there will be differences in the implementation but, they  will ALL declare there is a duplicate identifier in that code because, again and again and again ad nauseam, parameters and local variables are in the same scope.  If they weren't, they wouldn't be duplicates, even Homer Simpson can figure that out.   (There might be an exception which is, a compiler that includes code from you or maybe Thaddy, in which case I would suspicious about its implementation of boolean operator precedence too.. LOL)

Maybe this will help you (probably won't but religions claim miracles happen, so just in case):
Code: Pascal  [Select][+][-]
  1. procedure foo1(x: Integer);
  2. var
  3.  { the "x" below is in a different scope than the parameter "x"               }
  4.  
  5.  rec : record
  6.    x : Double; { NO duplicate identifier }
  7.  end;  
  8. begin
  9. end;
  10.  
  11.  
  12. procedure foo2(x: Integer);
  13. var
  14.  { the variable "x" is in the same scope as the parameter "x"                 }
  15.  
  16.  x: Double;  { duplicate identifier - what a surprise!!!... LOL               }
  17. begin
  18. end;
  19.  
in foo1, the identifier "x" is in a different scope than the parameter "x", as a result of that, no duplicate identifier is emitted by the compiler.

in foo2, the compiler emits a duplicate identifier message, the difference ?  in foo1, "x" is not in the same scope as the parameter "x".   Really, really simple.  Comprende ?

if not comprende yet, read this paragraph from "Compiler Construction" (page 7):
Quote
5  Symbol Table Handling

A symbol table maps the names in a program to their attributes such as their types and their addresses. It consists of several subtables (scopes), one for every module, procedure and record type. As a consequence of single pass compilation, which Wirth preferred in his later compilers, the scopes can be organized in a stack-like way. A new scope is opened at the top of the stack whenever the parser encounters a procedure, and it is closed when the parser has finished with this procedure.
In case you "no comprende" yet, that means, one scope per procedure block.
Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 16, 2025, 11:03:35 pm
The scope of parameters and local variables starts at the beginning of the procedure block and ends at the end of the procedure block.  Where their visibility starts is not the same.
Now you are just plainly contradicting your own source:
Quote
We must also specify in which part of a block an object can be referenced, This part of the block is called the scope of the object.
Brinch Hansen defines the part of a block where a object can be referenced (i.e. it's visibility) as the scope of the object.
You are now saying the exact oposite of the book you claim to be the authority on what a scope is
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 16, 2025, 11:21:44 pm
Quote
We must also specify in which part of a block an object can be referenced, This part of the block is called the scope of the object.
Brinch Hansen defines the part of a block where a object can be referenced (i.e. it's visibility) as the scope of the object.
Nice try but definitely not.  The visibility of an identifier is not the same as the scope in which it is defined.  The scope starts where the procedure block starts, the identifier's visibility starts once it is fully defined.  The scope contains the identifier definitions.

That scope contains all the local definitions which include, among other things, the function/procedure parameters.

It took PBH several hundred pages to explain basic language grammar and compiler construction concepts, you need to read the book to understand _all_ the concepts.  Reading 50 lines of FPC source code doesn't do it, much less when you grossly misinterpret the concepts it implements.

You want to talk about compilers, educate yourself, read some books!  That way you'll stop posting misleading garbage detrimental to forum readers.

Title: Re: Nested declarations inside advanced record
Post by: lainz on January 17, 2025, 12:00:00 am
And what about this valid program, I don't understand what's going on here:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. procedure a();
  4.   var c: integer = 1;
  5.  
  6.   procedure b();
  7.   var c: integer = 2;
  8.   begin
  9.     writeln(c);
  10.   end;
  11.  
  12. begin
  13.   writeln(c);
  14.   b();
  15.   writeln(c);
  16.   b();
  17. end;
  18.  
  19. begin
  20.   a();
  21.   readln;
  22.  
  23. end.
Title: Re: Nested declarations inside advanced record
Post by: TRon on January 17, 2025, 12:08:53 am
And what about this valid program, I don't understand what's going on here:

Let's try:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$h+}
  4.  
  5. procedure a();             // -> scope a
  6.   var c: integer = 1;
  7.  
  8.   procedure b();           // -> scope b
  9.   var c: integer = 2;
  10.   begin
  11.     writeln('scope b: ', c);
  12.   end;                     // <- scope b
  13.  
  14. begin                      // this scope a
  15.   writeln('scope a: ', c);
  16.   b();
  17.   writeln('scope a: ', c);
  18.   b();
  19. end;                       // <- scope a
  20.  
  21. begin                      // -> scope main
  22.   a();
  23.   readln;
  24. end.                       // <- scope main
  25.  

Code: Bash  [Select][+][-]
  1. scope a: 1
  2. scope b: 2
  3. scope a: 1
  4. scope b: 2
  5.  
Title: Re: Nested declarations inside advanced record
Post by: jamie on January 17, 2025, 01:38:10 am
Still at it I see. All it takes is one to destroy it for everyone else, be it this thread or other threads currently boiling.

 I've be kicking my heals lately if I should upgrade from D 11 to 12, not sure if the coast is worth it since that too seems to have some minor issues.
 But with his recent activity If I had to choose between two evals I think I'll upgrade.

  If there are individuals campaigning here to discourage the use of fpc/Lazarus, it's working.

 Jamie
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 17, 2025, 02:13:24 am
@lainz,

@TRon provided you a good explanation.

I want to offer one derived from the code you posted that is more complete.  Once you understand @TRon's explanation, tackle this one:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$h+}
  2.  
  3. program TRonScopes;
  4.  
  5. procedure a(ParameterA : integer);        // -> scope a
  6.          { ^ the procedure block and its corresponding scope start at the     }
  7.          {   open parenthesis.  This is important for 2 reasons: 1. it causes }
  8.          {   ParameterA NOT to be available outside of procedure "a" and 2.   }
  9.          {   it causes the parameter to be in the same scope as all other     }
  10.          {   definitions contained in the procedure block.                    }
  11.  
  12.   { ParameterA, "c", "k", "b" and "v" are all in the same scope which is what }
  13.   { allows procedure "a" to use them without qualifier.                       }
  14.  
  15.   var
  16.     c: integer = 1;
  17.  
  18.   const
  19.     k = 100;
  20.  
  21.  
  22.   { ------------------------------------------------------------------------- }
  23.   { nested procedures                                                         }
  24.  
  25.   procedure b(ParameterB : integer);      // -> scope b
  26.            { ^ start of procedure block and scope (nested in scope of "a")    }
  27.   var
  28.     d: integer = k;     { "k" from the outer scope is visible                 }
  29.  
  30.     c: integer = 2;
  31.                 { ^ "c" become visible after the ";", not before              }
  32.  
  33.     { note that "x" and "z" below are not visible yet even though they are in }
  34.     { the same scope (defined by "b"'s procedure block)                       }
  35.  
  36.   const
  37.     x = 3; { x is visible after the ";" but not before                        }
  38.     z = x; { this is ok because "x" is visible                                }
  39.  
  40.     { the definition below is erroneous and the compiler knows that because   }
  41.     { "y" has not been fully defined by the time it is used.                  }
  42.  
  43.     // y = y;  { uncomment to see the error "identifier not found"            }
  44.  
  45.   begin     { scopes "a" and "b" are visible                                  }
  46.  
  47.     d := d;                                // gets rid of "not used" hint
  48.  
  49.     writeln('scope b: ', ParameterA);      // ParameterA = 7
  50.     writeln('scope b: ', ParameterB);      // ParameterB = 3 and 33
  51.     writeln('scope b: ', c);               // c = 2
  52.   end;      { end of scope b                                                  }
  53.  
  54.   { "v" is in the same scope as the other identifiers defined in procedure "a"}
  55.   { but, because of _where_ it is defined, it is not visible to procedure "b" }
  56.   { IOW, scope <> visibility                                                  }
  57.   var
  58.     v : integer = 2;  { procedure "b" cannot see (thus not use) this variable }
  59.  
  60.   { end of nested procedures                                                  }
  61.   { ------------------------------------------------------------------------- }
  62.  
  63. begin       { scope "a" is visible ("b" is not)                               }
  64.  
  65.   writeln('scope a: ', c);                 // c = 1
  66.   b(3);
  67.   writeln('scope a: ', c);                 // c = 1
  68.   b(33);
  69.  
  70.   { procedure "b" is in the scope of procedure "a" BUT the ParameterB is NOT  }
  71.  
  72.   { uncomment the following to see "identifier not found" which proves that   }
  73.   { the parameter is in a different scope than the procedure name.            }
  74.  
  75.   // ParameterB := 1;
  76. end;                                       // <- scope a
  77.  
  78.  
  79. begin                                      // -> scope main/TRonScopes
  80.   a(7);
  81.   readln;
  82. end.                                       // <- scope main/TRonScopes
  83.  
This one includes parameters and identifier visibility.  It's just an "extension" of the code you presented.




But with his recent activity If I had to choose between two evals I think I'll upgrade.
Let them know you disapprove of how they implement the "with" statement, no doubt they'll "correct" that for you...

Happy upgrades to you!
Title: Re: Nested declarations inside advanced record
Post by: lainz on January 17, 2025, 03:21:30 am
Thanks. So is the same as in Javascript with the scopes.
@440bx why there's v and y? Seems that you wanted to type v in both places or something that I don't understand...

Edit: now I understand y = y is not possible. Done! The same if it was v = v in that scope...
Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 17, 2025, 04:15:30 am
Edit: now I understand y = y is not possible. Done! The same if it was v = v in that scope...
You got it.


Title: Re: Nested declarations inside advanced record
Post by: Warfley on January 18, 2025, 12:27:44 am
read some books!
You brought up a book and are now contradicting what is written black on white without any room for interpretation in the very same book you brought up.

I think I don't need book advice from you :)

And you know whats better than any list of books on compiler construction? Working on actual real world compilers. I worked with the sources of clang, fpc and a some compilers for domain specific languages used in the automitive industry. I worked with compilers used by millions and deployed in code for billion dollar projects.
You bring up some educational visualizations for a reference compiler that no one ever used for any productive code.

Books are great to learn the basics, but if you never go out and work on real world code, you can never go beyond a very shallow understanding.
Title: Re: Nested declarations inside advanced record
Post by: Joanna on January 18, 2025, 01:03:01 am
Still at it I see. All it takes is one to destroy it for everyone else, be it this thread or other threads currently boiling.
I've be kicking my heals lately if I should upgrade from D 11 to 12, not sure if the coast is worth it since that too seems to have some minor issues.
 But with his recent activity If I had to choose between two evals I think I'll upgrade.
 If there are individuals campaigning here to discourage the use of fpc/Lazarus, it's working.
 Jamie
Speaking of which I recently came across this thread in the mailing lists. https://www.mail-archive.com/fpc-pascal@lists.freepascal.org/msg57220.html
The discussion led to some bad ideas like “let’s change lazarus to be as bad as other projects that are popular ”. Finally nickolay pointed out that fpc and Lazarus are better than other languages he has used. So what is the problem then? {Apart from pascal usage being suppressed by wealthy people who hate it, which we can do nothing about}

Many people in our community are antisocial to put it mildly.. Why can’t programming be fun like it used to be? I learned so much just from chatting about pascal and other topics with Jamie and other people too. It’s like all the pascal programmers I used to socialize with have disappeared and the majority are just not too active forum accounts.

Then there are angry threads like this one. Is this the closest that we get to fun around here?

I’ll never forget the time long ago when a thread discussion which was quite pleasant and had many participants was suddenly locked upon the whim of a moderator who decided it was off topic. At least that thread was a positive experience that didn’t make the community look bad. I can’t say the same for many threads I’ve seen since...

I’ll never get over my disappointment about the lively discussion that was locked and lost opportunity to make friends and learn new things that will never happen again because apparently this type of thread is the best we can do now. We have become spectators of a fight rather than accomplishing something useful for making people want to use fpc.

Our community is not making using Fpc/Lazarus seem fun, it often seems More like angry frustrated people getting mad at each other. Who would want to be part of that?

Title: Re: Nested declarations inside advanced record
Post by: 440bx on January 18, 2025, 02:13:29 am
I think I don't need book advice from you :)
You need a lot more than advice.  As I said, educate yourself, hopefully after you gain some basic  knowledge you'll stop spreading your ignorance.  Start with PBH's On Pascal Compilers, you really need to get a grasp of the basics.

Title: Re: Nested declarations inside advanced record
Post by: PascalDragon on January 18, 2025, 04:57:51 pm
I’ll never forget the time long ago when a thread discussion which was quite pleasant and had many participants was suddenly locked upon the whim of a moderator who decided it was off topic. At least that thread was a positive experience that didn’t make the community look bad. I can’t say the same for many threads I’ve seen since...

If a thread is about XYZ and then starts to be about ABC then that drifting off must be stopped, because when someone searches for XYZ only to have to wade through pages of ABC then that person isn't helped. More so if after pages of ABC XYZ isn't even solved yet.

I’ll never get over my disappointment about the lively discussion that was locked and lost opportunity to make friends and learn new things that will never happen again because apparently this type of thread is the best we can do now. We have become spectators of a fight rather than accomplishing something useful for making people want to use fpc.

That's how forums are supposed to operate. If you don't like that, well, though luck.
Title: Re: Nested declarations inside advanced record
Post by: Joanna on January 20, 2025, 12:29:42 am
@pascaldragon
It’s possible to move a conversation that has gone off topic to a new thread instead of ruining it. I’ve seen it done before so I know it can be done.
Title: Re: Nested declarations inside advanced record
Post by: PascalDragon on January 20, 2025, 10:33:14 pm
@pascaldragon
It’s possible to move a conversation that has gone off topic to a new thread instead of ruining it. I’ve seen it done before so I know it can be done.

It's possible, yes, but it's up to the moderator to decide how they want to handle that.
TinyPortal © 2005-2018