Does declaration order matter in class? lazarus / 21-General ---------------------------------------------------------------------- jollytall · 2025-01-05 09:29 I have the following two declarations: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  tMyClass = class    public      procedure toto(foo : integer);    public      foo : integer;    end;    and Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  tMyClass = class    public      foo : integer;    public      procedure toto(foo : integer);    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? ---------------------------------------------------------------------- Packs · 2025-01-05 09:43 type   tMyClass = class    Private       foo : integer;     public       procedure toto(foo : integer);     end; Variable should be inside private ---------------------------------------------------------------------- Thaddy · 2025-01-05 09:44 It is intended behavior. What is allowed, though, is locals and globals wth the same name if {$modeswitch duplicatelocals} is active. ---------------------------------------------------------------------- Thaddy · 2025-01-05 09:47 Quote from: Packs on January 05, 2025, 10:43:47 amVariable 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. ---------------------------------------------------------------------- jollytall · 2025-01-05 10:56 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. ---------------------------------------------------------------------- Warfley · 2025-01-05 11:45 According to the documentation this is a bug: QuoteParameters 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 ---------------------------------------------------------------------- ALLIGATOR · 2025-01-05 11:54 @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 ) ---------------------------------------------------------------------- Paolo · 2025-01-05 12:41 Something more here https://forum.lazarus.freepascal.org/index.php/topic,61382.msg461595.h tml#msg461595 ---------------------------------------------------------------------- jollytall · 2025-01-05 15:33 @Warfley: I am not even sure what the quoted sentence from the documentation wanted to mean. "class methods" I understand as Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type tMyClass = classclass var foo : integer;class procedure toto(foo : integer);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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  tMyClass = class  public    class procedure toto(foo : integer);    class var fee : integer;  public    class property foo : integer read fee;  end;