Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Optional arguments

 [1/3] from: d4marcus::dtek::chalmers::se at: 17-May-2001 23:25


Long time ago, I recall there was a thread about handling optional arguments. I didn't realise at the time that Rebol overall has just as good support for this as LISP, but indeed it is different, and you also have be a bit careful with the unset! values. opt: func ['v] [either word? :v [if value? v [get :v]] [v]] test: func [a [any-type!] b [any-type!] c [any-type!]] [ reduce [opt a opt b opt c]] Now, the problem is to delimit the expression. Argument fetching is always greedy so you have to do this somehow. But it proves to be easy.
>> print test 1 probe to-word "hi!" ; this won't work
hi! ** Script Error: hi! has no value ** Near: hi! none
>> print (test 1) probe to-word "hi!" ; this works
1 none none hi! == hi!
>> (print test 1) probe to-word "hi!" ; this works as well
; you can do without parens if you give all arguments
>> print test () None 1 'done
none none 1 == done Instead of using parens (which resembles LISP a bit), you can (in good old Rebol tradition) use blocks:
>> print do [test 1] probe to-word "hi!"
1 none none hi! == hi! To my surprise, though the docs mention how to delimit expressions in regards of changing the order of evaluation, i.e. for math expr:
>> 1 + 2 * 3
== 9
>> 1 + (2 * 3)
== 7 , it doesn't mention that it can be used for functions with optional arguments as well. I think this ought to be included. At the moment I figure such functions are used mostly on the console, but there's really no reason why they couldn't be used in scripts too. You just have to remember to delimit them. A little LISP can be useful at times. :-) Marcus ------------------------------------ If you find that life spits on you calm down and pretend it's raining

 [2/3] from: agem:crosswinds at: 18-May-2001 15:27


Have a look at the rebol-style of options, func[/opt1 arg-opt1 /opt2 arg-opt2 ..] where no brackets are needed. Unhandy on console, but otherwise preferable. Volker
>>>>>>>>>>>>>>>>>> Ursprüngliche Nachricht <<<<<<<<<<<<<<<<<<
Am 17.05.01, 22:25:43, schrieb Marcus Petersson <[d4marcus--dtek--chalmers--se]> zum Thema [REBOL] Optional arguments:

 [3/3] from: d4marcus:dtek:chalmers:se at: 19-May-2001 0:54


On Fri, 18 May 2001, Volker Nitsch wrote:
> Have a look at the rebol-style of options, > func[/opt1 arg-opt1 /opt2 arg-opt2 ..] > where no brackets are needed. > Unhandy on console, but otherwise preferable.
Refinements? Yes, usually they make the function clearer. But why should we use two variables for an option where only one is needed? Oh well, it was only an idea. It works, so why not try it some time? Marcus ------------------------------------ If you find that life spits on you calm down and pretend it's raining