Recent

Author Topic: duplicate identifier  (Read 1042 times)

Paolo

  • Sr. Member
  • ****
  • Posts: 499
duplicate identifier
« on: November 27, 2022, 01:41:11 pm »
Hello (win 10, laz 2.2.2/fpc 3.2.2)

if I write this
Code: Pascal  [Select][+][-]
  1. procedure Test1;
  2. begin
  3.   beep;
  4. end;
  5.  
  6. procedure Test2;
  7.   procedure Test1;
  8.   begin
  9.     beep;
  10.   end;
  11. begin
  12.   Test1;
  13.   beep;
  14. end;
  15.  
  16.  

all is fine, and nested Test1 is called in Test2 execution

but if I write this, where procedures are part of Form1

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.Test1;
  3. begin
  4.   beep;
  5. end;
  6.  
  7. procedure TForm1.Test2;
  8.   procedure Test1;
  9.   begin
  10.     beep;
  11.   end;
  12. begin
  13.   beep;
  14. end;
  15.  

the compiler says : Duplicate identifier "test1".

That is right ?

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: duplicate identifier
« Reply #1 on: November 27, 2022, 01:51:55 pm »
Yes
Specialize a type, not a var.

Paolo

  • Sr. Member
  • ****
  • Posts: 499
Re: duplicate identifier
« Reply #2 on: November 27, 2022, 02:02:42 pm »
thanks Thaddy,

so Test1 in the second case does not overwrite the first Test1 declaration ?
why is not working as in case of simple procedure declaration ?

thans again.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: duplicate identifier
« Reply #3 on: November 27, 2022, 02:06:46 pm »
Not sure here, but usually {$MODE Delphi} is less strict than FPC regarding duplicate identifiers.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: duplicate identifier
« Reply #4 on: November 27, 2022, 02:10:22 pm »
That is right ?
That doesn't look right.  In the second case, Test1 is nested in Test2 just as it is in the first case.

Looks like a bug to me but, it will be nice to have one of the developers confirm that.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: duplicate identifier
« Reply #5 on: November 27, 2022, 02:26:04 pm »
Looks like a bug to me but, it will be nice to have one of the developers confirm that.
I also think it's a bug. Why is this allowed only in {$MODE DELPHI} mode, but not in {$MODE OBJFPC} mode.

Paolo

  • Sr. Member
  • ****
  • Posts: 499
Re: duplicate identifier
« Reply #6 on: November 27, 2022, 02:44:15 pm »
Quote
I also think it's a bug.

me too.

at the end the compiler should not be confused on which Test1 I am using and allows code like

Quote

procedure TForm1.Test1;
begin
  beep;
end;
 
procedure TForm1.Test2;
  procedure Test1;   
  begin
    beep;
  end;
begin
  Test1;        //the neseted one
  Self.Test1;  //the fom1.Test1
end;
 

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: duplicate identifier
« Reply #7 on: November 27, 2022, 03:05:02 pm »
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.

Curt Carpenter

  • Sr. Member
  • ****
  • Posts: 396
Re: duplicate identifier
« Reply #8 on: November 27, 2022, 08:11:14 pm »
For consistency, wouldn't a warning make more sense?  But that said, I can sure see how having multiple procedures with the same name might cause pain for someone trying to maintain a program.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: duplicate identifier
« Reply #9 on: November 27, 2022, 08:31:11 pm »
That is right ?
That doesn't look right.  In the second case, Test1 is nested in Test2 just as it is in the first case.

Looks like a bug to me but, it will be nice to have one of the developers confirm that.

Maybe the directive {$modeswitch duplicatelocals} can help somehow?

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: duplicate identifier
« Reply #10 on: November 27, 2022, 09:26:37 pm »
Maybe the directive {$modeswitch duplicatelocals} can help somehow?
I cannot help feeling uneasy about "artifices" to create exceptions to languages rules.   

I think there should not be exceptions to the scoping rules.  Their presence opens the door to inconsistencies and confusing situations (e,g. why does it accept "this or that" in "this case" and  not in "that case" when the two cases are essentially identical ?)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: duplicate identifier
« Reply #11 on: November 27, 2022, 09:31:14 pm »
For consistency, wouldn't a warning make more sense?  But that said, I can sure see how having multiple procedures with the same name might cause pain for someone trying to maintain a program.

People like to ignore warnings even if they're useful ones. Errors can't be ignored however.

Maybe the directive {$modeswitch duplicatelocals} can help somehow?

Yes, that modeswitch controls that and is enabled by default in mode Delphi.

Maybe the directive {$modeswitch duplicatelocals} can help somehow?
I cannot help feeling uneasy about "artifices" to create exceptions to languages rules.   

I think there should not be exceptions to the scoping rules.  Their presence opens the door to inconsistencies and confusing situations (e,g. why does it accept "this or that" in "this case" and  not in "that case" when the two cases are essentially identical ?)

This error has already saved me from potential issues and it arises more likely with object oriented programming.

This functionality is not a bug and it won't be changed.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: duplicate identifier
« Reply #12 on: November 27, 2022, 09:38:13 pm »
This functionality is not a bug and it won't be changed.
Since from what you're saying it's by design then it may not be a bug but, bug or not, it is an inconsistency in the language's scoping rules.  Inconsistencies are not desirable.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Paolo

  • Sr. Member
  • ****
  • Posts: 499
Re: duplicate identifier
« Reply #13 on: November 27, 2022, 09:42:35 pm »
I agree with 440bx. Anyway if it is by design, in my view, also the first code fragment should rise the same warning/error.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: duplicate identifier
« Reply #14 on: November 27, 2022, 09:48:09 pm »
I agree with 440bx. Anyway if it is by design, in my view, also the first code fragment should rise the same warning/error.

No, because that would raise issues with backwards compatibility. Object Pascal support was added later to FPC (with the transition from version 1.x to 2.x) and the restriction for the identifier names was added from the get go there, but it couldn't be added for global scope without potentially breaking exisiting code.

 

TinyPortal © 2005-2018