如何将函数文字作为回调传递

How to pass a function literal as a callback

这是我正在尝试做的事情:

void x(function int(int) f){
    f(555);
}

void main(){
    x(function int(int q){  });
}

错误信息令人困惑:

funcs.d(4): Error: basic type expected, not function
funcs.d(4): Error: found 'int' when expecting '('
funcs.d(4): Error: basic type expected, not (
funcs.d(4): Error: function declaration without return type. (Note that constructors are always named 'this')
funcs.d(4): Error: found 'f' when expecting ')'

我无法从此类错误消息中得到任何信息。

将 return 类型替换为 x 中的 function 关键字。出于某种原因,它们在文字上是相反的。此外,您传递的函数不会 return 任何东西,即使它应该

void x(int function(int) f){
    f(555);
}

void main(){
    x((int q){ return 0; });
    // or
    x(function int(int q){ return 0; });
    // or
    x(q => 0);
}