Recent

Author Topic: Nested declarations inside advanced record  (Read 21874 times)

440bx

  • Hero Member
  • *****
  • Posts: 4981
Re: Nested declarations inside advanced record
« Reply #60 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!

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1317
Re: Nested declarations inside advanced record
« Reply #61 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 ?
« Last Edit: January 15, 2025, 12:28:22 am by Joanna from IRC »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

440bx

  • Hero Member
  • *****
  • Posts: 4981
Re: Nested declarations inside advanced record
« Reply #62 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1317
Re: Nested declarations inside advanced record
« Reply #63 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
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Warfley

  • Hero Member
  • *****
  • Posts: 1863
Re: Nested declarations inside advanced record
« Reply #64 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
« Last Edit: January 15, 2025, 01:01:09 am by Warfley »

ASerge

  • Hero Member
  • *****
  • Posts: 2373
Re: Nested declarations inside advanced record
« Reply #65 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: 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.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1317
Re: Nested declarations inside advanced record
« Reply #66 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.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Thaddy

  • Hero Member
  • *****
  • Posts: 16516
  • Kallstadt seems a good place to evict Trump to.
Re: Nested declarations inside advanced record
« Reply #67 on: January 15, 2025, 06:00:28 am »
Scope is nothing more than an isolation level. ASerge at al. refuse to get that.
But I am sure they don't want the Trumps back...

Warfley

  • Hero Member
  • *****
  • Posts: 1863
Re: Nested declarations inside advanced record
« Reply #68 on: January 15, 2025, 06:01:46 pm »
What the "level"?
From FPC doc Scope: 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 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

440bx

  • Hero Member
  • *****
  • Posts: 4981
Re: Nested declarations inside advanced record
« Reply #69 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Warfley

  • Hero Member
  • *****
  • Posts: 1863
Re: Nested declarations inside advanced record
« Reply #70 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 :)

440bx

  • Hero Member
  • *****
  • Posts: 4981
Re: Nested declarations inside advanced record
« Reply #71 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lainz

  • Hero Member
  • *****
  • Posts: 4684
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Nested declarations inside advanced record
« Reply #72 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 =)

440bx

  • Hero Member
  • *****
  • Posts: 4981
Re: Nested declarations inside advanced record
« Reply #73 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lainz

  • Hero Member
  • *****
  • Posts: 4684
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Nested declarations inside advanced record
« Reply #74 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-)
« Last Edit: January 15, 2025, 09:51:35 pm by lainz »

 

TinyPortal © 2005-2018