Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate identifier of property and method parameter of a class

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.

like image 575
Ivan Myagky Avatar asked Feb 04 '23 09:02

Ivan Myagky


1 Answers

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.

like image 73
Rudy Velthuis Avatar answered Apr 27 '23 09:04

Rudy Velthuis