在 Red/Rebol 中调用此函数时如何评估函数的细化

How to evaluate refinement of a function when calling this function in Red/Rebol

>> f: func [x /a][either a [x + 2] [x + 1]]
== func [x /a][either a [x + 2] [x + 1]]

>> b: /a
== /a

>> f/b 1
*** Script Error: f has no refinement called b
*** Where: f
*** Stack: f  

>> f/:b 1
*** Script Error: f has no refinement called :b
*** Where: f
*** Stack: f  

可以看到函数f有一个refinement a,我把/a绑定到b。当通过 b 对其细化 /a 调用 f 时,它失败了。

将需要先评估的细化传递给其函数的正确方法是什么?或者,有没有办法将 path! 转换为 function!

改进是有限的,在某种程度上,它们只能通过在函数调用中逐字列出来传递。另一方面,您可以根据需要以任何方式构造此类函数调用(path! 值后跟所有参数):

>> b: 'a
== a
>> do probe reduce [to path! reduce ['f b] 1]
[f/a 1]
== 3

请注意,本例中的路径元素是 word!,而不是 refinement!。通常,apply 涵盖了此类用例。然而,只有 Rebol 本身就有它,并且有一个笨拙的调用约定:

>> f: func [x /a][either a [x + 2] [x + 1]]
>> b: yes
== true
>> apply :f [1 b]
== 3
>> apply :f [1 no]
== 2  

如果需要,您可以轻松编写自己的高级版本。