D: unwanted anonymous function attributes

Consider the following pattern:

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

It works:

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

However, this does not 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 considered safe @nogc , and the explicit sort_arr not.

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


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

+5
source share
1 answer

I think this is an error with attribute output from inline properties. You can report this to the D issue tracker, http://issues.dlang.org/ .

However, note that the built-in .sort property / function is on its way to obsolescence. Instead, use std.algorithm.sort , which should not have this problem.

+3
source

Source: https://habr.com/ru/post/1202647/


All Articles