Arguments and attributes with the same name

bearophile bearophileHUGS at lycos.com
Thu Mar 4 04:59:12 PST 2010


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



More information about the Digitalmars-d mailing list