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

[REBOL] Optional arguments

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