Recent

Author Topic: A question about duplicatelocals  (Read 1184 times)

Zoran

  • Hero Member
  • *****
  • Posts: 1459
    • http://wiki.lazarus.freepascal.org/User:Zoran
A question about duplicatelocals
« on: February 24, 2018, 07:51:58 pm »
1.
  - In Delphi mode method parameters can have same name as class members.
  - In ObjFpc mode method parameters cannot have same name as class members.
This can be changed by adding {$modeswitch duplicatelocals}...

However,
2.
  - In Delphi mode local variables in methods can have same name as class members.
  - In ObjFpc mode local variables in methods cannot have same name as class members.
Unlike (1), this cannot be changed with {$modeswitch duplicatelocals}!

This compiles (delphi mode):
Code: Pascal  [Select]
  1. program Project1;
  2.  
  3. {$mode delphi}
  4.  
  5. type
  6.   TClass1 = class(TObject)
  7.   public
  8.     X: Integer;
  9.     procedure Proc1(X: Integer);
  10.     procedure Proc2;
  11.   end;
  12.  
  13. // Parameter has same name as member field.
  14. // Compiles in delphi mode
  15. procedure TClass1.Proc1(X: Integer);
  16. begin
  17. end;
  18.  
  19. // local variable has same name as member field.
  20. // Compiles in delphi mode
  21. procedure TClass1.Proc2;
  22. var
  23.   X: Integer;
  24. begin
  25. end;
  26.  
  27. begin
  28. end.    
  29.  

But this does not compile (objfpc mode with duplicatelocals):
Code: Pascal  [Select]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$ModeSwitch duplicatelocals}
  5.  
  6. type
  7.   TClass1 = class(TObject)
  8.   public
  9.     X: Integer;
  10.     procedure Proc1(X: Integer);
  11.     procedure Proc2;
  12.   end;
  13.  
  14. // Parameter has same name as member field.
  15. // This is okay with duplicatelocals
  16. procedure TClass1.Proc1(X: Integer);
  17. begin
  18. end;
  19.  
  20. // Local variable has same name as member field.
  21. // Unlike with delphi mode, this is compiler error, even with duplicatelocals!
  22. procedure TClass1.Proc2;
  23. var
  24.   X: Integer;
  25. begin
  26. end;
  27.  
  28. begin
  29. end.    
  30.  

Why so?
I would expect that duplicatelocals allows that...

Can you please comment on this, why duplicatelocals doesn't allow this? Is this the decision made for some reason, or something that was just missed to implement?
« Last Edit: February 24, 2018, 07:56:59 pm by Zoran »

ASerge

  • Hero Member
  • *****
  • Posts: 1406
Re: A question about duplicatelocals
« Reply #1 on: February 24, 2018, 08:14:18 pm »
I think this is a bug.

Zoran

  • Hero Member
  • *****
  • Posts: 1459
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: A question about duplicatelocals
« Reply #2 on: February 24, 2018, 08:45:42 pm »
I think this is a bug.

Okay, thank you. I am going to report it then.

Zoran

  • Hero Member
  • *****
  • Posts: 1459
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: A question about duplicatelocals
« Reply #3 on: February 24, 2018, 08:59:19 pm »
Reported: bug 33221