Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D: undesired anonymous function attributes

Tags:

lambda

d

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

like image 608
Amir Abiri Avatar asked Oct 20 '22 01:10

Amir Abiri


1 Answers

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.

like image 74
Vladimir Panteleev Avatar answered Oct 23 '22 02:10

Vladimir Panteleev