Well since I know you're thinking about asking for the solution I had come
up with, I'll go all out and post it here. The actual solution to loop
through the function items (refinements) is not much code, but is tedious
and messy-looking. I feel like I'm going through alot of trouble to hide
this and save a few characters in the "high-level" function, but the result
is a little easier on the eye and made me learn about contexts and 'bind.

; Say you have a high-level function (main-func) with a list of
refinements, and you want to pass all of these refinements to a lower-level
function (sub-func). The bare parameter-passing code is:

main-func: function [/va /vb /vc][this-code refine-values r][
     this-code: make-code "main-func" "sub-func"
     bind this-code 'some-global bind this-code 'refine-values
     do this-code
]

sub-func: function [param-list][va vb vc][
     set [va vb vc] param-list
     print ["va:" va]
     print ["vb:" vb]
     print ["vc:" vc]
]

; Resulting in:
>> main-func/va/vc
va: true
vb: none
vc: true

; To support this you must define the following globally:

some-global: none
make-code: function [hi-func [string!] lo-func [string!]][code][
     code: copy {
          refine-values: make block! []
          foreach r first :hi-func [
               if (refinement? r) and (r <> /local) [
                    append refine-values get bind to-word r 'refine-values
               ]
          ]
          lo-func refine-values
     }
     replace/all code "hi-func" hi-func
     replace/all code "lo-func" lo-func
     return(to-block code)
]

- Michael Jelinek





[EMAIL PROTECTED] on 08/08/2000 07:05:12 AM

From: [EMAIL PROTECTED] on 08/08/2000 07:05 AM

Please respond to [EMAIL PROTECTED]
To:   [EMAIL PROTECTED]
cc:
Subject:  [REBOL] Passing refinements to sub functions.


Hi list,

Often I have a function built on another. Sometimes the lower function has
refinement that I want to surface on the higher function. If there is one
refinement I just use an either. If there are more this approach becomes a
real pain.

I've come up the solution of creating an object with the refinements as
fields and passing that. But I still have a bit of code creating the object
in the first place. An alternative is using a block with set words and
values, but I haven't figured how to use this effectively (that is I
suspect
I need
bind but I haven't got it working yet).

Has anyone an elegant solution for passing multiple refinements down the
line?

Brett.

--
>> my-rebol-stuff
== http://www.zipworld.com.au/~bhandley/rebol










Reply via email to