lyl*_*lyl 1 rebol rebol3 rebol2 red
>> 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
Run Code Online (Sandbox Code Playgroud)
您可以看到该函数f有一个refinement a, 我绑定/a到b。当通过进行f细化调用时,它会失败。/ab
传递需要在其功能之前进行评估的细化的正确方法是什么?或者,有没有办法将 a 转换path!为function!?
细化是有限的,只能通过在函数调用中逐字列出来传递。另一方面,您可以path!按照您想要的方式构造这样的函数调用(一个值后跟所有参数):
>> b: 'a
== a
>> do probe reduce [to path! reduce ['f b] 1]
[f/a 1]
== 3
Run Code Online (Sandbox Code Playgroud)
请注意,本例中路径的元素是word!s,而不是refinement!s。一般来说,此类用例包含在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
Run Code Online (Sandbox Code Playgroud)
如果您需要的话,您可以轻松编写自己的高级版本。