Re: Constructor params with same name as members

2014-10-24 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/23/14 3:22 PM, Marc =?UTF-8?B?U2Now7x0eiI=?= schue...@gmx.net wrote: On Thursday, 23 October 2014 at 18:57:33 UTC, Ali Çehreli wrote: On 10/22/2014 11:37 PM, Jonathan M Davis wrote: x = x; which is so obviously wrong that I don't know how much of anyone could make that mistake. But

Re: Constructor params with same name as members

2014-10-23 Thread Shriramana Sharma via Digitalmars-d-learn
Oh OK here it is: http://dlang.org/deprecate.html#Variable%20shadowing%20inside%20functions But it says it is an error to use the feature by 2.061 and I'm using 2.066! Doesn't the scope of that deprecation cover struct members too? In general it should be like variables in inner scopes should

Re: Constructor params with same name as members

2014-10-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, October 23, 2014 11:47:04 Shriramana Sharma via Digitalmars-d-learn wrote: Oh OK here it is: http://dlang.org/deprecate.html#Variable%20shadowing%20inside%20functions But it says it is an error to use the feature by 2.061 and I'm using 2.066! Doesn't the scope of that

Re: Constructor params with same name as members

2014-10-23 Thread bearophile via Digitalmars-d-learn
Jonathan M Davis: Questions like this have come up and been discussed before, but using the same parameter names as member variable names for constructors is such a common practice that there would be quite a bit of screaming if we didn't allow it. I'm willing to hear them scream. D should

Re: Constructor params with same name as members

2014-10-23 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/23/14 1:03 AM, 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 ; } } void main() { auto P = Pair(1, 2) ; writeln(P.x, ' ', P.y) ; } This

Re: Constructor params with same name as members

2014-10-23 Thread Ali Çehreli via Digitalmars-d-learn
On 10/22/2014 11:37 PM, Jonathan M Davis wrote: x = x; which is so obviously wrong that I don't know how much of anyone could make that mistake. But simply making it illegal to assign a variable to itself would solve that problem, and that arguably should be done, since it's a essentially

Re: Constructor params with same name as members

2014-10-23 Thread via Digitalmars-d-learn
On Thursday, 23 October 2014 at 18:57:33 UTC, Ali Çehreli wrote: On 10/22/2014 11:37 PM, Jonathan M Davis wrote: x = x; which is so obviously wrong that I don't know how much of anyone could make that mistake. But simply making it illegal to assign a variable to itself would solve that

Re: Constructor params with same name as members

2014-10-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, 23 October 2014 at 18:57:33 UTC, Ali Çehreli wrote: On 10/22/2014 11:37 PM, Jonathan M Davis wrote: x = x; which is so obviously wrong that I don't know how much of anyone could make that mistake. But simply making it illegal to assign a variable to itself would solve that

Constructor params with same name as members

2014-10-22 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. Please see the following code: import std.stdio ; struct Pair { int x, y ; this (int x, int y) { x = x ; y = y ; } } void main() { auto P = Pair(1, 2) ; writeln(P.x, ' ', P.y) ; } This outputs 0 0, whereas the equivalent C++ code outputs 1 2 correctly: #

Re: Constructor params with same name as members

2014-10-22 Thread H. S. Teoh via Digitalmars-d-learn
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

Re: Constructor params with same name as members

2014-10-22 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I perfectly realize that it's not advisable to take advantage of shadowing. In fact, I asked the question because I thought D specifically *didn't* allow shadowing, but here it is, being silently permitted to mishappen... I seem to read to have read that (D didn't allow shadowing) but I'm

Re: Constructor params with same name as members

2014-10-22 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Oct 23, 2014 at 11:01:10AM +0530, Shriramana Sharma via Digitalmars-d-learn wrote: Hello. I perfectly realize that it's not advisable to take advantage of shadowing. In fact, I asked the question because I thought D specifically *didn't* allow shadowing, but here it is, being silently