在 Rebol 中通过细化获取 Object 'Func

发布于 2024-08-14 12:10:21 字数 420 浏览 4 评论 0原文

假设我有

o: context [

  f: func[message /refine message2][
    print [message] 
    if refine [print message 2]
  ]

]

我可以这样称呼它

do get in o 'f "hello"

但是我该怎么做才能细化呢?像这样的东西会起作用

>> do get in o 'f/refine "hello" "world"
** Script Error: in expected word argument of type: any-word
** Near: do get in o 'f/refine
>>

Let's say I have

o: context [

  f: func[message /refine message2][
    print [message] 
    if refine [print message 2]
  ]

]

I can call it like this

do get in o 'f "hello"

But how can I do for the refinement ? something like this that would work

>> do get in o 'f/refine "hello" "world"
** Script Error: in expected word argument of type: any-word
** Near: do get in o 'f/refine
>>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不奢求什么 2024-08-21 12:10:21

我不知道是否有办法直接告诉解释器在调用函数值时使用细化。当它的参数是一个函数时,这将需要对do进行一些参数化!类似的东西似乎不存在......但也许它隐藏在其他地方。

我知道使用细化的唯一方法是使用路径。为了清楚起见,我将首先使用一个临时词:

>> fword: get in o 'f
>> do compose [(to-path [fword refine]) "hello" "world"]  
hello
world

组合后第二条语句的计算结果是:

do [fword/refine "hello" "world"]

您实际上也可以将函数值放入路径中。它消除了对中介的需要:

>> do compose [(to-path compose [(get in o 'f) refine]) "hello" "world"]
hello
world

PS,上面的 message2 之间有一个额外的空格,它应该只是 message2

I don't know if there's a way to directly tell the interpreter to use a refinement in invoking a function value. That would require some parameterization of do when its argument is a function! Nothing like that seems to exist...but maybe it's hidden somewhere else.

The only way I know to use a refinement is with a path. To make it clear, I'll first use a temporary word:

>> fword: get in o 'f
>> do compose [(to-path [fword refine]) "hello" "world"]  
hello
world

What that second statement evaluates to after the compose is:

do [fword/refine "hello" "world"]

You can actually put function values into paths too. It gets rid of the need for the intermediary:

>> do compose [(to-path compose [(get in o 'f) refine]) "hello" "world"]
hello
world

P.S. you have an extra space between message and 2 above, where it should just be message2

如若梦似彩虹 2024-08-21 12:10:21

执行此操作:

o/('f)/refine "hello" "world"

如果路径表达式中的括号对应于对象字段或系列拾取/戳索引引用,则对它们进行求值。这使得上面的代码等效于:

apply get in o 'f ["hello" true "world"]

请注意,apply 参数是位置性的,因此您需要知道声明参数的顺序。您无法通过函数细化本身来实现该技巧,因此如果您想参数化函数调用的细化,则必须使用 apply 或创建路径表达式来评估。

Do this:

o/('f)/refine "hello" "world"

Parens in a path expression are evaluated if they correspond to object field or series pick/poke index references. That makes the above code equivalent to this:

apply get in o 'f ["hello" true "world"]

Note that apply arguments are positional, so you need to know the order the arguments were declared in. You can't do that trick with the function refinements themselves, so you have to use apply or create path expressions to evaluate if you want to parameterize the refinements of the function call.

君勿笑 2024-08-21 12:10:21

使用简单路径 o/f/refine

Use the simple path o/f/refine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文