[fpc-devel] Local variable names and colision with attributes/properties names

Marcos Douglas md at delfire.net
Thu Sep 27 16:48:50 CEST 2012


On Thu, Sep 27, 2012 at 11:30 AM,  <michael.vancanneyt at wisa.be> wrote:
>
>
> On Thu, 27 Sep 2012, Marcos Douglas wrote:
>
>> On Thu, Sep 27, 2012 at 11:01 AM, Jonas Maebe <jonas.maebe at elis.ugent.be>
>> wrote:
>>>
>>>
>>> On 27 Sep 2012, at 15:51, Marcos Douglas wrote:
>>>
>>> What is the advantages to the compiler return the error bellow?
>>> ERROR: unit1.pas(31,3) Error: Duplicate identifier "FooVar"
>>>
>>> Why the compiler do not respect the scope, ie, the local variable
>>> should have the preference, I'm not right?
>>>
>>>
>>> Yes, but the human brain is very good at confusing such things, and it's
>>> very easy to accidentally assign something to a local variable instead of
>>> to
>>> a field.
>>
>>
>> Only Pascal uses that way?
>
>
> No, the brain works that way.
>
> The FPC team decided to protect you from accidental mistakes by forbidding
> this special case in $mode objfpc.
>
> For me, it has become second nature never ever to have a variable with the
> same name as a property, even in Delphi.

How do you know if you are using a function or method?
For example, I can code a StrToInt method in my class. You can extend
it. The "StrToInt method" is now in other unit and you "can't see it".
Now you code:
blablabla
  i := StrToInt(x);
blablabla

Which StrToInt are you using? How our brains could help here?
BUT, if you use:
Self.StrToInt(x)
... you know what is happening.

But this is a programmer's choice, ie, using or not the Self. The
problem, IMHO, is that I can't choose when we talk about local
variables.

Marcos Douglas



More information about the fpc-devel mailing list