Remove closure allocation

Malte no at valid.mail
Sat May 26 15:00:40 UTC 2018


I was trying to get a function that has a closure allocation to 
compile with @nogc.

Assuming this function:
>int identity(immutable int q) pure nothrow @safe
>{
>    import std.algorithm;
>
>    static immutable auto arr = [42];
>    int getSecondArgument(int a, int b)
>    {
>        return b;
>    }
>
>    return arr.map!(a => getSecondArgument(a,q))[0];
>}
When I add @nogc it complains about using q.
My idea was to change the closure to have another parameter with 
default initializer:
>int identity(immutable int q) pure nothrow @safe @nogc
>{
>    import std.algorithm;
>
>    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];
>}

This compiles with DMD, however it returns random numbers instead 
of the value I passed in. Looks like a bug to me. Should that 
work or is there any other pattern I could use for that?


More information about the Digitalmars-d-learn mailing list