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

2012-10-04 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=7603



--- Comment #2 from Andrej Mitrovic andrej.mitrov...@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: ---


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

2012-02-27 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=7603


bearophile_h...@eml.cc changed:

   What|Removed |Added

 CC||bearophile_h...@eml.cc


--- Comment #1 from bearophile_h...@eml.cc 2012-02-27 19:56:57 PST ---
Ow.

Even this fails:

void test1(ref int val = 10) {}
void test2(out int val = 20) {}
void main() {
int x;
test1(x);
assert(x == 10);
test2(x);
assert(x == 20);
}

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