Bug 5850 – Default arguments of out and ref arguments
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-04-16T18:11:00Z
Last change time
2012-12-11T01:38:32Z
Keywords
wrong-code
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2011-04-16T18:11:29Z
This D2 program compiles with no errors and runs raising no assert errors:
void foo(out int x=1, ref int y=2) {}
void main() {
int x, y;
foo(x, y);
assert(x == 0 && y == 0);
}
If default arguments for out and ref arguments can't be made to work, then I suggest to disallow them statically.
In Ada (2012) "A default_expression is only allowed in a parameter_specification for a formal parameter of mode in." See point 19 here:
http://www.ada-auth.org/standards/12rm/html/RM-6-1.html
Comment #1 by andrej.mitrovich — 2012-12-11T01:38:32Z
This is related to Issue 7603.
Not that your sample won't compile anymore. What will compile is this:
int outerX = 1;
int outerY;
void foo(out int x = outerX, ref int y = outerY) { y = 2; }
void main()
{
int innerX, innerY;
foo(innerX, innerY);
assert(innerX == 0 && innerY == 2);
assert(outerX == 1 && outerY == 0);
foo();
assert(outerX == 0 && outerY == 2);
}
So 'out' and 'ref' default arguments refer to what variables are referenced, not what values are written. Maybe you should open a documentation enhancement request so the above is added to the docs to clear out any confusion for newbies.
*** This issue has been marked as a duplicate of issue 7603 ***