[Issue 3878] Arguments and members with the same name

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 26 04:57:20 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=3878



--- Comment #13 from bearophile_hugs at eml.cc 2013-11-26 04:57:12 PST ---
(In reply to comment #12)

> In reply to the original report, I disagree that this is a significant issue,
> in fact I'll often deliberately name a parameter the same as a member.
> 
> struct S {
>     int m;
>     this(int m) { this.m = m; }
> }

Thank you for the answer. From my experience that code is very bug prone, I
have found various bugs in code like that. I am not the only D programmer to
hit such problem.

Two alternative solutions:

1) To allow to refer to the instance fields in the constructor signature, so
you don't need to repeat names (something like this is present in Scala and
Typescript):

class S {
    int m;
    this(in this.m) {}
}


2) Require the usage of "this." in constructors, so you have to write this:

struct S {
    int m;
    this(int m) { this.m = m; }
}


The problem is not just about constructors, but they are typically the ones
where you initialise most instance members using arguments, so it's where I had
my bugs.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list