4

I transferred my project from Delphi to Lazarus. In a form I have a private method with parameter var Active: Boolean. In Delphi it was ok, but Lazarus give an error Error: Duplicate identifier "Active" and Hint: Identifier already defined in unit FORMS at line 641, on line 641 there is:

property Active: Boolean read FActive;

It is not difficult to change parameter name (with refactoring), but why can't I use the same name for property and parameter of method?
To make sure it is not an error of automatic conversion from Delphi, I created new project in Lazarus and added private method

procedure Test(var Active: Boolean);

The result was the same. Even if I use const or nothing instead of var. I've looked into FPC docs and didn't find any such limitations. I'm just curious.

1 Answers1

8

You should be able to use the same name for a property and a parameter. They have different scope, so the one nearest in scope (the parameter, which should be treated as being in the same scope as a local variable) should hide the one "further away" in scope (the property). In Delphi, you can still access the property, even inside that method, but then you should qualify it as Self.Active:

procedure TForm1.Test(var Active: Boolean);
var
  ParamActive: Boolean;
  FormActive: Boolean;
begin
  ParamActive := Active;      // gets the var parameter
  FormActive := Self.Active;  // gets the property
  ...
end;

I have no idea why FPC flags it as an error. It shouldn't.

Update

FWIW, if you change

{$mode objfpc}

to

{$mode delphi}

It does compile as expected, and you won't get an error. I just tried this.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • 2
    yup, it is a feature of $mode objfpc – Marco van de Voort May 24 '17 at 21:32
  • Found it in docs now. For `mode Delphi` it says "Parameters in class methods can have the same names as class properties (although it is bad programming practice)." and this behavior forbidden in `mode objfpc`. Thanks. – Ivan Myagky May 25 '17 at 06:25
  • Bad programming practice? Nonsense! Didn't they ever hear of variable scoping? In Pascal, and many other languages, it is normal that a local variable or parameter hides an identifier not as near in scope. Plain Pascal doesn't have objects, but a parameter still hides a global variable of the same name. That is not bad programming practice, it is how it should be. – Rudy Velthuis May 25 '17 at 06:44
  • @MarcovandeVoort: I wouldn't call it a feature. It is bad design. – Rudy Velthuis May 25 '17 at 06:49
  • That's an opinion. My point with feature is that it was deliberately done (as can be seen that it works in Delphi). I personally don't like mode objfpc too much either though. It is a bit too much "different to be different" with perceived benefits IMHO in mostly hypothetical situations, without IMHO enough gain to warrant the difference. – Marco van de Voort May 25 '17 at 13:08
  • No, that is not an opinion. It is like that in Pascal, it is like that in most other languages. It is how it should be. It is very normal that a local variable (including a parameter) hides a variable of the same name further away in scope.It is not even bad programming practice. I have no idea why anyone would think so. – Rudy Velthuis May 25 '17 at 15:14
  • FWIW, I have no doubt it was deliberately done. But is wrong anyway. – Rudy Velthuis May 25 '17 at 15:16