Recent

Author Topic: Error: Duplicate identifier XXX when method param has same name of variable  (Read 1284 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #15 on: November 29, 2022, 12:05:54 pm »
Bottom Line: Any identifier (Field, Method, Property) within a class can only occur once, except overloads for Methods.
It has to do with Scope of identifier

Ok but a method param should not conflict with class members. Am I wrong?
Consider this code (compiles in Delphi-Mode)
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode delphi}
  3. Uses Classes, SysUtils;
  4. type
  5.  
  6.   { TExampleClass }
  7.  
  8.   TExampleClass = class(TObject)
  9.   public
  10.     Field: string;
  11.     Function Func:String;
  12.     Procedure SomeMethod(Func:String);
  13.   end;
  14.  
  15. Var
  16.   c:TExampleClass;
  17.  
  18. function TExampleClass.Func: String;
  19. begin
  20.   Result:='I''m a Function-Result';
  21. end;
  22.  
  23. procedure TExampleClass.SomeMethod(Func: String);
  24. Var
  25.   s:String;
  26. begin
  27.   s:=Func;
  28.   Writeln(s);
  29. end;
  30.  
  31. begin
  32.   c:=TExampleClass.Create;
  33.   c.SomeMethod('Test');
  34.   c.Free;
  35. end.
What would you expect to be written in the console?

Bottom Line for me: DON'T!!!!
That's a hideyhole for serious bugs.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #16 on: November 29, 2022, 12:09:00 pm »
Bottom Line: Any identifier (Field, Method, Property) within a class can only occur once, except overloads for Methods.
It has to do with Scope of identifier

Ok but a method param should not conflict with class members. Am I wrong?
Consider this code (compiles in Delphi-Mode)
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode delphi}
  3. Uses Classes, SysUtils;
  4. type
  5.  
  6.   { TExampleClass }
  7.  
  8.   TExampleClass = class(TObject)
  9.   public
  10.     Field: string;
  11.     Function Func:String;
  12.     Procedure SomeMethod(Func:String);
  13.   end;
  14.  
  15. Var
  16.   c:TExampleClass;
  17.  
  18. function TExampleClass.Func: String;
  19. begin
  20.   Result:='I''m a Function-Result';
  21. end;
  22.  
  23. procedure TExampleClass.SomeMethod(Func: String);
  24. Var
  25.   s:String;
  26. begin
  27.   s:=Func;
  28.   Writeln(s);
  29. end;
  30.  
  31. begin
  32.   c:=TExampleClass.Create;
  33.   c.SomeMethod('Test');
  34.   c.Free;
  35. end.
What would you expect to be written in the console?

Bottom Line for me: DON'T!!!!
That's a hideyhole for serious bugs.

I expect the value of the input parameter, as, in that scope, it takes precedence on the class member. This is why one can write Self.Func to access to the class member in a unequivocable way.

If the reason why fpc does not allows this, I believe is a good I idea to prevent it, only I would understand if it is so, because IMHO blocking also method params is more than what I would expect.

« Last Edit: November 29, 2022, 12:23:15 pm by tt »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #17 on: November 29, 2022, 12:14:47 pm »
OK, now i understand.
You'll probably have to wait for one of the dev's to answer this, since i never "reuse" identifiers within the same class
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #18 on: November 29, 2022, 12:37:15 pm »
Stop.

I found an answer by marcov, just few days ago. Stupid me.

It's  deliberate. The idea was that it would avoid accidental mistakes, at the cost of a small limitation. But it wasn't applied to Delphi because of compatibility.

FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #19 on: November 29, 2022, 12:42:42 pm »
Bottom Line: Any identifier (Field, Method, Property) within a class can only occur once, except overloads for Methods.
Yes, thats how it was teached me too, maybe thats, even when it was with Delphi, I never tried to use again and again same identifiers/duplicates.
And that something can compile does not mean that it works like you expect it.
Code: Pascal  [Select][+][-]
  1. function x(const x:string):string;
  2. begin
  3.   x := x; // which x is what?
  4. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #20 on: November 29, 2022, 01:10:20 pm »
Bottom Line: Any identifier (Field, Method, Property) within a class can only occur once, except overloads for Methods.
Yes, thats how it was teached me too, maybe thats, even when it was with Delphi, I never tried to use again and again same identifiers/duplicates.
And that something can compile does not mean that it works like you expect it.
Code: Pascal  [Select][+][-]
  1. function x(const x:string):string;
  2. begin
  3.   x := x; // which x is what?
  4. end;

It seems I started the "how to write weird code contest".

if function X was in class X it would be ze top.

Code: Pascal  [Select][+][-]
  1.  
  2. function X.X(const x: string): X;
  3.  

Marcov's answer is the real answer I was looking for.
« Last Edit: November 29, 2022, 01:20:18 pm by tt »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #21 on: November 29, 2022, 01:30:43 pm »
And that something can compile does not mean that it works like you expect it.

One thing is that it compiles (so it is syntactically correct), another is that makes what programmer wants.

In my example code was "delphi" correct and it did exactely was one would expect. And it was also intelligible to the reader (presence of Self. was not by chance).

In your example instead it was impossible to understand unless you put artificial precedence rules. And the reaon is that pascal syntax allows methods to omit the parenteses, so that methods cann have the same appearance of variables.

FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #22 on: November 29, 2022, 01:40:24 pm »
Bottom Line: Any identifier (Field, Method, Property) within a class can only occur once, except overloads for Methods.
Yes, thats how it was teached me too, maybe thats, even when it was with Delphi, I never tried to use again and again same identifiers/duplicates.
And that something can compile does not mean that it works like you expect it.
Code: Pascal  [Select][+][-]
  1. function x(const x:string):string;
  2. begin
  3.   x := x; // which x is what?
  4. end;

It seems I started the "how to write weird code contest".

if function X was in class X it would be ze top.

Code: Pascal  [Select][+][-]
  1.  
  2. function X.X(const x: string): X;
  3.  

Marcov's answer is the real answer I was looking for.
:D

Okay, to explain what my basic thinking with that dupe kind style if writing should do
method x gets as result the input x. so x := x  :-X
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #23 on: November 29, 2022, 01:50:01 pm »
Does anybody know why?
Yes. Because D7 should never have allowed your case in point.
The reason objpas mode is more strict - in many ways - is to prevent programmers making mistakes.
Also as this is the case here: do never name a field the same as a parameter: that is a duplicate identifier!!. Whether some versions of Delphi allow it is not really the issue.
The issue is that Delphi versions vary between releases and so does FPC.
In your case it is obvious that Delphi never should have allowed it.
In {$mode delphi} there are some more places where the Delphi engineers made a wrong decision and our compiler team worked around it. (e.g. Like automatic pointer dereferences)
To summarize, Delphi is not the holy grail: just like FPC it contains errors, both real and logic errors. As do most softwares. (Like yours  :o :D)
In the end, you already got proper advice by others, simply swallow it and follow it because as far as I can tell it is proper advice. No need to add an extra example and it is certainly not a bug.
« Last Edit: November 29, 2022, 01:58:37 pm by Thaddy »
Specialize a type, not a var.

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #24 on: November 29, 2022, 02:00:09 pm »
Does anybody know why?
Yes. Because D7 should never have allowed it.
The reason objpas mode is more strict - in many ways - is to prevent programmers making mistakes.
Also as this is the case here: do never name a field the same as a parameter: that is a duplicate identifier!!. Whether some versions of Delphi allow it is not really the issue.
The issue is that Delphi versions vary between releases and so does FPC.
In your case it is obvious that Delphi never should have allowed it.
In {$mode delphi} there are some more places where the Delphi engineers made a wrong decision and our compiler team worked around it. (e.g. Like automatic pointer dereferences)
To summarize, Delphi is not the holy grail: just like FPC it contains errors, both real and logic errors. As do most softwares. (Like yours  :o :D)
In the end, you already got proper advice by others, simply swallow it and follow it because as far as I can tell it is proper advice. No need to add an extra example.

I do not swallow a thing.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #25 on: November 29, 2022, 02:06:19 pm »
I do not swallow a thing.
In Britain and the Netherlands we used a cane applied to the back side of stubborn students... :o
Слава Україні!

Then again I might better have written swallow your pride, be humble and follow proper advice.
« Last Edit: November 29, 2022, 02:13:33 pm by Thaddy »
Specialize a type, not a var.

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #26 on: November 29, 2022, 02:29:26 pm »
Then again I might better have written swallow your pride, be humble and follow proper advice.

No.

Instead, I will put a cane on my back, donkey ears, activate {$delphi} mode and start writing code like KodeZwerg suggested as counterexample.

I will send u pictures.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #27 on: November 29, 2022, 02:40:35 pm »
Instead, I will put a cane on my back, donkey ears, activate {$delphi} mode
:o
and start writing code like KodeZwerg suggested as counterexample.
Please not use me  >:D or my stupid bad example instead use my answer.
Несіть Україні славу, а не ганьбу  O:-)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #28 on: November 29, 2022, 02:46:39 pm »
Please not use me  >:D or my stupid bad example .

 :)

Just to clarify: the reason why sometimes I don't follow the conventions proposed is because I want to avoid certain code impedence that comes from it (at the risk of confusing identifiers? maybe, I put attention to avoid this errors in other ways). So nothing against it nor against the "deliberate choice" in the objfpc mode to force identifiers to be "everywhere different".
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Error: Duplicate identifier XXX when method param has same name of variable
« Reply #29 on: November 29, 2022, 03:12:43 pm »
No, you are right. :)
Specialize a type, not a var.

 

TinyPortal © 2005-2018