[Issue 7603] Default initializer is allowed on ref/out params

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Oct 4 10:40:23 PDT 2012


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



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2012-10-04 10:34:29 PDT ---
(In reply to comment #0)
> void test1(ref bool val = true) {
> }
> 
> void test2(out bool val = true) {
> }

Actually I'm only half-right. Phobos uses this in std.random:

void randomShuffle(Range, RandomGen = Random)(Range r, ref RandomGen gen =
rndGen);

So 'val' is either a reference to a passed-in argument, or a reference to the
default argument which is an lvalue.

Default argument for 'ref' parameter should only be allowed if the argument is
an lvalue ('true' is an rvalue'). The compiler already checks this but *only*
if the function is called without arguments:

void test(ref bool val = true) { }

void main()
{
    bool b;
    test(b); // ok, no errors
    test();  // errors only on this invocation
}

The compiler should error as soon as it sees the invalid declaration. I'll make
a pull shortly.

Btw, the same applies to 'out'. An rvalue isn't valid, but an lvalue as default
argument is perfectly valid:

bool w = true;
void test(out bool val = w) { }

void main()
{
    test();
    assert(w == false);
}

-- 
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