Recent

Author Topic: Unit's name collides with variable name, but not function name  (Read 576 times)

TCH

  • Sr. Member
  • ****
  • Posts: 256
    • Oldschool computer
Unit's name collides with variable name, but not function name
« on: January 20, 2025, 04:37:11 pm »
teststufff.pas:
Code: Pascal  [Select][+][-]
  1. unit teststufff;
  2.  
  3. interface
  4.  
  5. procedure teststufff;
  6.  
  7. implementation
  8.  
  9. procedure teststufff;
  10. begin
  11. end;
  12.  
  13. end.
fpc teststufff.pas:
Code: [Select]
Free Pascal Compiler version 3.2.2 [2021/07/09] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling teststufff.pas
13 lines compiled, 0.0 sec
teststuffv.pas:
Code: Pascal  [Select][+][-]
  1. unit teststuffv;
  2.  
  3. interface
  4.  
  5. var teststuffv: integer;
  6.  
  7. implementation
  8.  
  9. end.
fpc teststuffv.pas:
Code: [Select]
Free Pascal Compiler version 3.2.2 [2021/07/09] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling teststuffv.pas
teststuffv.pas(5,15) Error: Duplicate identifier "teststuffv"
teststuffv.pas(7,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode
Why does the name of a variable collide with the unit's name? If it is because of "identifier", then why does not the function's name colliding with it? I do not understand the logic.

Also, how can i prevent/avoid this collision? The names have to remain the same.

Thaddy

  • Hero Member
  • *****
  • Posts: 16523
  • Kallstadt seems a good place to evict Trump to.
Re: Unit's name collides with variable name, but not function name
« Reply #1 on: January 20, 2025, 04:44:15 pm »
This is documented.
The unit name can't be used in the same unit.
Quote
The names have to remain the same.
can't be done.
You can escape a var with &, but you can't use the unit name inside that unit.
(I think it is actually a bug that it works for the procedure name, that should also fail.)
« Last Edit: January 20, 2025, 04:58:41 pm by Thaddy »
But I am sure they don't want the Trumps back...

ASerge

  • Hero Member
  • *****
  • Posts: 2379
Re: Unit's name collides with variable name, but not function name
« Reply #2 on: January 20, 2025, 07:54:24 pm »
can't be done.
Can. Even so:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. procedure Unit1;
  8.  
  9. implementation
  10.  
  11. procedure Unit1;
  12.  
  13.   procedure Unit1;
  14.   begin
  15.   end;
  16.  
  17. begin
  18. end;
  19.  
  20. end.

Although it's unusual for me. Delphi 7 does not allow this.

Thaddy

  • Hero Member
  • *****
  • Posts: 16523
  • Kallstadt seems a good place to evict Trump to.
Re: Unit's name collides with variable name, but not function name
« Reply #3 on: January 20, 2025, 07:56:31 pm »
Yes,
Unfortunately it does.
But it does not allow the variable as I explicitly pointed out! Grr.
But I am sure they don't want the Trumps back...

TCH

  • Sr. Member
  • ****
  • Posts: 256
    • Oldschool computer
Re: Unit's name collides with variable name, but not function name
« Reply #4 on: January 20, 2025, 09:04:50 pm »
This is documented.
The unit name can't be used in the same unit.
Quote
The names have to remain the same.
can't be done.
You can escape a var with &, but you can't use the unit name inside that unit.
(I think it is actually a bug that it works for the procedure name, that should also fail.)
It is intentional? Why is this so? The scope's name should not collide with the items'.  As a matter of fact, in case of functions, one can even use the scope:
Code: Pascal  [Select][+][-]
  1. teststufff.teststufff;
And that works.

 

TinyPortal © 2005-2018