www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5850] New: Default arguments of out and ref arguments

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5850

           Summary: Default arguments of out and ref arguments
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 16 2011
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5850


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrej.mitrovich gmail.com
         Resolution|                            |DUPLICATE



01:38:32 PST ---
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 ***

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