[REBOL] Re: Newbie question about func refinements
From: tim-johnsons::web::com at: 13-Aug-2008 14:23
On Wednesday 13 August 2008, Ed O'Connor wrote:
> I'm sure this has been covered elsewhere, but I couldn't find it. Given a
> function, such as:
>
> sum: func ["Return the sum of two numbers."
> arg1 [number!] "first number"
> arg2 [number!] "second number"
> /avg "return the average of two numbers"
> /sub "subtracts the first from the second"
> /div "divides the first into the second"
> ][
> switch/default self/refinement [
> avg [arg1 + arg2 / 2]
> sub [arg1 - arg2]
> div [arg1 / arg2]
> ][arg1 + arg2]
> ]
>
> Is there a way to dynamically obtain a refinement value so I can use it in
> a switch statement?
> In the example above, the pseudo-code expression "self/refinement" is what
> I'm looking for.
>
> Odd that I've never encountered this over the years. I'm sure someone here
> can set me straight on it.
Terribly blue-collar of me to propose an alternative, but simple solution.
Maybe a refinement is not what you want but a "mode" argument
Untested code: sum: func[arg1[number!] arge[number!] mode[word! unset!]
;; ...... check for unset? mode and assign default value if necessary.
switch/default mode [
'avg [arg1 + arg2 / 2]
'sub [arg1 - arg2]
'div [arg1 / arg2]
][arg1 + arg2]
HTH
Tim