Search code examples
lambdad

D: undesired anonymous function attributes


Consider the following template:

mixin template test(void function() callback)
{
    static this()
    {
        callback();
    }
}

This works:

mixin test!(&sort_arr);
void sort_arr()
{
    arr.sort;
}

However this doesn't work:

mixin test!({ arr.sort; });

DMD gives the following error:

Error: safe function 'main.__lambda6' cannot call system function '_adSort'
Error: @nogc function 'main.__lambda6' cannot call non-@nogc function '_adSort'

It seems to me that the lambda version is inferred to be safe @nogc, while the explicit sort_arr isn't.

How can I overcome this and pass an anonymous lambda to this template?


Edit: bug report filed as per the recommendation in the accepted answer: https://issues.dlang.org/show_bug.cgi?id=13481


Solution

  • I think this is a bug with inferring attributes from built-in properties. You can report it at D's issue tracker, at http://issues.dlang.org/.

    However, note that the built-in .sort property/function is on its way to being deprecated. Please use std.algorithm.sort instead, which shouldn't have this issue.