tags:

views:

44

answers:

2

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
>>
A: 

What is stopping you using the simple path o/f/refine ?

Peter W A Wood
Because I want to call within my new function here http://reboltutorial.com/blog/creating-a-class-based-oop-language-adding-class-constructors-support/ a constructor with a refinement. Since it must apply to any Object, I need to call by variable.I want to call the constructor automatically.
Rebol Tutorial
+1  A: 

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

Hostile Fork