Duplicate class method property and parameter identifier

I have transferred my project from Delphi to Lazarus. In a form, I have a private method with a parameter var Active: Boolean

. In Delphi it was fine, but Lazarus gave 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's not hard to change the parameter name (with refactoring), but why can't I use the same name for the property and method parameter?
To make sure this is not an automatic conversion error from Delphi, I created a new project in Lazarus and added a private method

procedure Test(var Active: Boolean);

      

The result was the same. Even if I use const

or nothing instead var

. I looked through the FPC docs and found no such restrictions. I'm just curious.

+3


source to share


1 answer


You should be able to use the same name for the property and parameter. They have different scopes, so the one closest to the scope (the parameter that should be considered in the same scope as the local variable) should hide the one "further" in the scope (property). In Delphi, you can still access the property even inside this method, but then you have to qualify it like 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 is flagging it as a bug. It shouldn't.

Update

FWIW if you change



{$mode objfpc}

      

to

{$mode delphi}

      

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

+7


source







All Articles