调用不同块中定义的函数 (Red/Rebol)

Calling function defined in the different block (Red/Rebol)

我想知道如何调用这样定义的函数:

outer: [inner: [ f: func [p1 p2] [rejoin ["p1:" p1 "/p2:" p2]] ]]

print outer/inner/f "aaa" "bbb"

func

如果我在函数调用前添加 do,我会得到

*** Script Error: func is missing its spec argument

有人能指出问题所在吗?

你写的是block!的数据。数据在被评估之前不会变成代码。您可能想将 block! 变成 object!。试试这个:

>> outer: object [inner: object [ f: func [p1 p2] [rejoin ["p1:" p1 "/p2:" p2]] ]]
== make object! [
    inner: make object! [
        f: func [p1 p2][rejoin ["p1:" p1 "/p2:" p2]]
    ]
]
>> print outer/inner/f "aaa" "bbb"
p1:aaa/p2:bbb

在 Rebol 中,默认情况下不评估块(例如 do [...]if true [...])。在您的示例中,outer/inner/f 返回未评估的词 'func。如果您希望以这种方式放置函数,则需要选择一种创建方式,例如:

outer: compose/deep [inner: [f: (func [p1 p2][...])]]

或者:

outer: [inner: [f: none]]
outer/inner/f: func [p1 p2][...]