www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Arguments and attributes with the same name

reply bearophile <bearophileHUGS lycos.com> writes:
This is correct D code, but I think it's a bad practice to write code like
this, where method arguments have the same name as instance/static attributes
of the class/struct.

class Foo {
    int x;
    this(int x) { this.x = x; }
    void inc(int x) { this.x += x; }
}
struct Bar {
    static int x;
    void inc(int x) { Bar.x += x; }
}
void main() {}


In Python this problem is less important because the language forces you to
always prepend the argument names with "self.", while in D the "this." is
optional.

In the last years D has added errors for situations where you use duplicated
names, for example in nested scopes or in with statement.

So do you think it's a good idea to forbid/warn against such name clash?

Bye,
bearophile
Mar 04 2010
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
bearophile wrote:
 This is correct D code, but I think it's a bad practice to write code like
this, where method arguments have the same name as instance/static attributes
of the class/struct.
 
 class Foo {
     int x;
     this(int x) { this.x = x; }
     void inc(int x) { this.x += x; }
 }
 struct Bar {
     static int x;
     void inc(int x) { Bar.x += x; }
 }
 void main() {}
 
 
 In Python this problem is less important because the language forces you to
always prepend the argument names with "self.", while in D the "this." is
optional.
 
 In the last years D has added errors for situations where you use duplicated
names, for example in nested scopes or in with statement.
 
 So do you think it's a good idea to forbid/warn against such name clash?

This can be solved if the compiler warns when an assignment is from a variable to the same one. x = x; // error: assignment has no effect
Mar 04 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Ary Borenszweig:

 This can be solved if the compiler warns when an assignment is from a 
 variable to the same one.
 
 x = x; // error: assignment has no effect

That's nice in general to have, but I think it's not enough, because if I want to assign to an attribute an incremented value: x = x+1; What is that x? The attribute or the argument? Bye, bearophile
Mar 04 2010
parent Ary Borenszweig <ary esperanto.org.ar> writes:
bearophile wrote:
 Ary Borenszweig:
 
 This can be solved if the compiler warns when an assignment is from a 
 variable to the same one.

 x = x; // error: assignment has no effect

That's nice in general to have, but I think it's not enough, because if I want to assign to an attribute an incremented value: x = x+1; What is that x? The attribute or the argument?

class Foo: def __init__(self, x): x = x + 1 This compiles just fine in pyhtoh. What is x here? The argument, of course, because that's how it works in python. You forgot to put self before x. class Foo { int x; this(int x) { x = x + 1; } } What is x here? The argument, of course, because that's how it works in python. You forgot to put this before x. How is python safer than D in that sense?
Mar 04 2010