Arguments and attributes with the same name
Steven Schveighoffer
schveiguy at yahoo.com
Thu Mar 4 09:03:00 PST 2010
On Thu, 04 Mar 2010 11:49:13 -0500, bearophile <bearophileHUGS at lycos.com>
wrote:
> Steven Schveighoffer:
>> I think the point is, the statement x = x + 1; isn't any more or less
>> ambiguous in D than it is in python. There is a well-defined meaning
>> (and
>> in fact the same meaning).
>
> It's not the same meaning, this program prints "1":
>
> import std.c.stdio: printf;
> class Foo {
> int x;
> this() {}
> void foo() {
> x = x + 1;
> }
> }
> void main() {
> Foo f = new Foo();
> f.foo();
> printf("%d\n", f.x); // prints 1
> }
>
> An equivalent Python program with "x = x + 1;" prints:
> UnboundLocalError: local variable 'x' referenced before assignment
> Because the usage of "self." is obligatory.
That's not the ambiguity being discussed. What is being discussed is when
there is a local parameter with the same name as an instance field. In
that context, (at least from what I read in this thread) D and python are
the same.
Anyway, it's not important. I agree that it may be good to disallow this
from happening in the first place.
-Steve
More information about the Digitalmars-d
mailing list