[Issue 6442] Allow for passing values with the 'ref' keyword

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Aug 7 13:31:06 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=6442



--- Comment #22 from bearophile_hugs at eml.cc 2011-08-07 13:30:59 PDT ---
I have a related but alternative proposal; to ask only for the "out" at the
calling point, make it obligatory if you compile with -warning and optional
otherwise (for a long time "override" was like this).

I think having "out" at the calling point is more useful than "ref".

Currently D 2.054 gives no warnings/errors on a program like this (I think the
C# compiler gives something here):


void foo(out int x) {
    x = 5;
}
void main() {
    int y = 10;
    foo(y);
}


The problem here is the initialization of y to 10 always gets ignored.
Assigning something to y, *not using y in any way*, and then using it in a
"out" function argument call, is code smell. It's wasted code at best, and
sometimes it's related to possible semantic bugs.

Using "out" at the calling point doesn't fix that code, but helps the
programmer to see that the assign of 10 to y is useless, and it's better to
remove it:


void foo(out int x) {
    x = 5;
}
void main() {
    int y = 10;
    foo(out y);
}


In my opinion "ref" arguments don't have the same need of being tagged at the
calling point because a function that uses "ref" often reads and writes the
argument (otherwise you use "in" or "out"), so in a ref argument assigning
something to y before the call is more often meaningful:


void foo(ref int x) {
    x++;
}
int main() {
    int y = 10;
    return foo(y);
}

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


More information about the Digitalmars-d-bugs mailing list