我试图将函数作为参数传递给REBOL编程语言,但我还没有想出正确的语法:
doSomething: func [a b] [
a b
a b
]
doSomething print "hello" {This should pass print as the first argument and "hello" as the second argument.}
Run Code Online (Sandbox Code Playgroud)
这会产生错误,因为print正在调用函数而不是传递函数:
hello
*** ERROR
** Script error: doSomething does not allow unset! for its a argument
** Where: try do either either either -apply-
** Near: try load/all join %/users/try-REBOL/data/ system/script/args...
Run Code Online (Sandbox Code Playgroud)
是否可以将print函数作为参数传递而不是调用print函数?
我找到了解决方案:我只需要:在作为参数传递的函数名称之前添加.
这里,:print函数作为参数传递,而不是以"hello"作为参数调用:
doSomething: func [a b] [
a b
a b
]
doSomething :print "hello" {This should pass print as the first argument and "hello" as the second argument.}
Run Code Online (Sandbox Code Playgroud)