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

标签 function lambda delegates arguments d

使用 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

最佳答案

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

为了进一步解释,我将向您展示如果您将其扩展为更长的委托(delegate)形式而不是使用简写 =>,它将如何变化。 :

(Event event) => { writeln(variableFromParentScope, event); }

变成

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

如您所见,您正在返回一个在实际委托(delegate)中没有参数的委托(delegate)。如果您删除 => ,您的委托(delegate)将按预期工作。

您的委托(delegate)参数的替代有效形式是:

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

仅当您想返回某些东西时,才使用 =>箭。 () => { something } 的用例将是一个委托(delegate)返回一个委托(delegate)(就像一个委托(delegate)为给定的输入生成委托(delegate))

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

关于function - D (dlang) 将 lambda 函数作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58231207/

相关文章:

c# - 使用反射来指定委托(delegate)的类型(附加到事件)?

iOS 委托(delegate)不使用 UIViewControllers

oracle - 将 Crystal Report 命令参数传递给 Oracle 函数

function - 将更新的函数传递给现有函数

c++ - 将 const 函数引用绑定(bind)到 lambda

lambda - 小阴谋家 : stuck on multiinsertLR&co example

ios - 如何在 Objective-C 中创建委托(delegate)?

javascript - 搜索功能不会过滤所有值

function - 解释 SML 函数及其类型

java - 如何使用 IntStream 对 int 数组的特定索引数求和?