Arguments and attributes with the same name

Clemens eriatarka84 at gmail.com
Thu Mar 4 09:17:11 PST 2010


bearophile Wrote:

> Ary Borenszweig:
> > So you suggest to forbid having an argument name that 
> > matches an attribute name? Or require this. for attribute access?
> 
> I prefer the compiler to give an error when an argument has the same name of an attribute, it's tidier.

I'd rather not have that, I like to write my constructors in the way you have shown in your first post:

class Foo {
    int x;
    this(int x) { this.x = x; }
}

Saves me from inventing silly names for the parameters (like "theX") or appending ugly pre-/suffixes like an underscore.

> 
> If you think that solution is too much restricting, there's a second solution: in only those case, where there can be ambiguity for the eyes of a programmer the compiler can require the use of "this.".

I don't understand this suggestion. As soon as you have an instance variable x and a function parameter x, there will *always* be ambiguity (unless you don't use the parameter). Requiring the use of "this." amounts to disallowing access to the parameter x. Or do you suggest making the distinction based on whether x is used as an lvalue or a rvalue?



More information about the Digitalmars-d mailing list