D:不期望的匿名函数属性

Ami*_*iri 5 lambda d

考虑以下模板:

mixin template test(void function() callback)
{
    static this()
    {
        callback();
    }
}
Run Code Online (Sandbox Code Playgroud)

这有效:

mixin test!(&sort_arr);
void sort_arr()
{
    arr.sort;
}
Run Code Online (Sandbox Code Playgroud)

但是这不起作用:

mixin test!({ arr.sort; });
Run Code Online (Sandbox Code Playgroud)

DMD给出以下错误:

Error: safe function 'main.__lambda6' cannot call system function '_adSort'
Error: @nogc function 'main.__lambda6' cannot call non-@nogc function '_adSort'
Run Code Online (Sandbox Code Playgroud)

在我看来,lambda版本被推断为safe @nogc,而显式sort_arr不是.

我怎样才能克服这一点并将匿名lambda传递给此模板?


编辑:根据接受的答案中的建议提交的错误报告:https://issues.dlang.org/show_bug.cgi?id = 13481

Vla*_*eev 3

我认为这是从内置属性推断属性的错误。您可以在 D 的问题跟踪器(http://issues.dlang.org/)上报告该问题。

但是,请注意,内置.sort属性/函数即将被弃用。请改用std.algorithm.sort它,应该不会出现此问题。