Lazarus

Programming => General => Topic started by: jollytall on January 05, 2025, 10:29:34 am

Title: Does declaration order matter in class?
Post by: jollytall on January 05, 2025, 10:29:34 am
I have the following two declarations:
Code: Pascal  [Select][+][-]
  1. type
  2.   tMyClass = class
  3.     public
  4.       procedure toto(foo : integer);
  5.     public
  6.       foo : integer;
  7.     end;    
and
Code: Pascal  [Select][+][-]
  1. type
  2.   tMyClass = class
  3.     public
  4.       foo : integer;
  5.     public
  6.       procedure toto(foo : integer);
  7.     end;
The first one compiles and runs happily (as I would expect, inside the procedure the argument foo is used, everywhere else the member foo), but the second fails (what I also sort-of understand that it is not good programming practice to use the same identifier twice - although it is done a lot with global and local variables elsewhere in the language).
What I find strange and incorrect that the way it is handled (again, I think both can be explained) depends on the sequence of the declaration. Is it intended behavior or a bug?
Title: Re: Does declaration order matter in class?
Post by: Packs on January 05, 2025, 10:43:47 am
type
  tMyClass = class
   Private
      foo : integer;
    public
      procedure toto(foo : integer);
    end;

Variable should be inside private
Title: Re: Does declaration order matter in class?
Post by: Thaddy on January 05, 2025, 10:44:35 am
It is intended behavior.
What is allowed, though, is locals and globals wth the same name if {$modeswitch duplicatelocals} is active.
Title: Re: Does declaration order matter in class?
Post by: Thaddy on January 05, 2025, 10:47:31 am
Variable should be inside private
They usually are but it is not a requirement.
Your code does not work either: the duplicate use is the cause of the error and that is by design.
Title: Re: Does declaration order matter in class?
Post by: jollytall on January 05, 2025, 11:56:31 am
If it is intended behavior then why is it not detected in the other case? I find it an error that simply the sequence of declaration changes how it works (or does not work in one case).

@Packs: As said by Thaddy it is not a requirement. Honestly, if a simple member can be read and wrote from outside, I do not see why to make it a property with GetValue, SetValue like set-up. I think it does not always improve the code readability.
Title: Re: Does declaration order matter in class?
Post by: Warfley on January 05, 2025, 12:45:05 pm
According to the documentation (https://www.freepascal.org/docs-html/prog/progse76.html) this is a bug:
Quote
Parameters in class methods cannot have the same names as class properties.
It's mainly a big that the check only checks identifiers registered in the class so far, not in the future/checks against Parameters.

I actually fixed that bug in a MR: https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/878
But it breaks every larger code base, because it's very easy to "abuse" this bug
Title: Re: Does declaration order matter in class?
Post by: ALLIGATOR on January 05, 2025, 12:54:18 pm
@Warfley
It's only in ObjFPC mode, right? Your patch

It has to be done at some point... the inevitable... better sooner than later. And correct any mistakes that arise )
Title: Re: Does declaration order matter in class?
Post by: Paolo on January 05, 2025, 01:41:44 pm
Something more here
https://forum.lazarus.freepascal.org/index.php/topic,61382.msg461595.html#msg461595
Title: Re: Does declaration order matter in class?
Post by: jollytall on January 05, 2025, 04:33:07 pm
@Warfley: I am not even sure what the quoted sentence from the documentation wanted to mean. "class methods" I understand as
Code: Pascal  [Select][+][-]
  1. type tMyClass = class
  2. class var foo : integer;
  3. class procedure toto(foo : integer);
  4. end;
and not as methods of a random class, as in the original post.

Also, it says that parameters of a method (class method or method of a random class, either way) cannot be the same as a property (class property or property of a random class does not matter again). So, it says nothing about of a member of a class.

Anyway, even class property and parameter of a class method compiles if used in the accepted sequence:
Code: Pascal  [Select][+][-]
  1. type
  2.   tMyClass = class
  3.   public
  4.     class procedure toto(foo : integer);
  5.     class var fee : integer;
  6.   public
  7.     class property foo : integer read fee;
  8.   end;
  9.  
TinyPortal © 2005-2018