D (dlang) 将 lambda 函数作为参数传递

D (dlang) passing a lambda function as argument

对于 D,我如何传递一个函数(可能是对函数的引用)作为参数在其他函数中执行?

import std.stdio : writeln;

class Event {}

class EventTarget
{
    void addEventListener(string eventName, void delegate(Event event) callback)
    {
        // TODO: add to slice to execute later, for now execute directly
        callback();
    }
}

void main()
{
    auto variableFromParentScope = "lorem ipsum";
    auto target = new EventTarget();
    target.addEventListener("load", (Event event) => { writeln(variableFromParentScope, event); }, true);
}

给我错误:

onlineapp.d(10): Error: delegate callback(Event event) is not callable using argument types ()
onlineapp.d(10):        missing argument for parameter #1: Event event
onlineapp.d(18): Error: function onlineapp.EventTarget.addEventListener(string eventName, void delegate(Event event) callback) is not callable using argument types (string, void delegate() @system delegate(Event event) pure nothrow @safe, bool)
onlineapp.d(18):        cannot pass argument __lambda1 of type void delegate() @system delegate(Event event) pure nothrow @safe to parameter void delegate(Event event) callback

我在这里设置了示例:https://run.dlang.io/is/FnQoId


解决方案,在答案的帮助下,我将其修复为:

import std.stdio : writeln;

class Event {}

class EventTarget
{
    void addEventListener(string eventName, void delegate(Event event) callback)
    {
        // TODO: add to slice to execute later, for now execute directly
        callback(new Event());
    }
}

void main()
{
    auto variableFromParentScope = "lorem ipsum";
    auto target = new EventTarget();
    target.addEventListener(
        "load", 
        (Event event) {
            writeln(variableFromParentScope, event);
        }
    );
}

工作示例:https://run.dlang.io/is/6aDRoU

target.addEventListener("load", (Event event) => { writeln(variableFromParentScope, event); }, true);

(Args) => {} 是一个 lambda,而 returns 是一个 lambda。

正确的形式:

  • target.addEventListener("load", (Event event) { writeln(variableFromParentScope, event); }, true);

  • target.addEventListener("load", (Event event) => writeln(variableFromParentScope, event), true);

您对委托使用了错误的语法,您还可以在错误消息中看到它没有预期的类型。

为了进一步解释,我将向您展示如果将其扩展为更长形式的委托而不是使用 shorthand =>:

,它会如何变化
(Event event) => { writeln(variableFromParentScope, event); }

变成

(Event event) { return { writeln(variableFromParentScope, event); }; }

如您所见,您正在 return 实际委托中没有任何参数的委托。如果您删除 =>,您的委托将按预期工作。

委托参数的替代有效形式为:

(event) { ... }
delegate (Event event) { ... }
delegate (event) { ... }
&someMemberMethod // some object member method taking in Event as first parameter
toDelegate(&someGlobalFunction) // from std.functional

仅当您想要 return 某些内容时才使用 => 箭头。 () => { something } 的一个用例是委托 returning 委托(就像委托为给定输入生成委托)

但是你的问题还有一个错误是你在调用参数中使用 , true 调用该函数,这使得错误消息非常混乱,并且你没有将事件参数传递给回调,这将是代码片段中的另一个错误。