On Thu, Oct 23, 2014 at 10:33:53AM +0530, Shriramana Sharma via Digitalmars-d-learn wrote: > Hello. Please see the following code: > > import std.stdio ; > struct Pair { > int x, y ; > this (int x, int y) { x = x ; y = y ; }
Please don't write code like this. How is the compiler supposed to know that "x = x" is supposed to mean "this.x = x", as opposed to "x = x" or "this.x = this.x" or "x = this.x"??! I used to enjoy writing this kind of code by cleverly exploiting subtle differences in symbol lookup rules, but eventually I realized that this kind of coding style is riddled with subtle errors, because shadowing member variables can lead to nasty mistakes (writing 'x' in your member functions can sometimes mean this.x, sometimes a function parameter or local variable) and makes the code unmaintainable (anybody else reading the code may not be aware of the subtle lookup rules you're exploiting to make the code work) and fragile (code changes like adding/removing a local variable may inadvertently change the meaning of 'x' in another line of code). Generally, shadowing member variables with parameter names is a Bad Idea(tm). Instead, my advice is to adopt a naming convention to distinguish between them, e.g.: struct S { int x, y; this(int _x, int _y) { x = _x; // now no longer ambiguous y = _y; } } T -- Don't modify spaghetti code unless you can eat the consequences.