www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18909] New: Lambda with default initializer gets called with

https://issues.dlang.org/show_bug.cgi?id=18909

          Issue ID: 18909
           Summary: Lambda with default initializer gets called with
                    random values instead
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: Malwas3 aol.com

The following code should print 2 but writes random numbers instead:

void main()  safe
{
    import std.stdio : writeln;

    writeln(identity(2));
}

int identity(immutable int q) pure nothrow  safe  nogc
{
    import std.algorithm : map;

    static immutable auto arr = [42];
    int getSecondArgument(int a, int b)
    {
        return b;
    }

    return arr.map!((int a, int b = q) => getSecondArgument(a, b))[0];
}

--
May 26 2018