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

[REBOL] Re: How to check function arguments ?

From: jason:cunliffe:verizon at: 9-Jun-2002 9:21

hmm.. Q: How to tell if a word already exists or if is just an argument value being passed?
>> somefunc: func [inp [any-type!]][if value? 'inp [print inp "do stuff"]] >> somefunc
== none
>> somefunc "hello"
hello == "do stuff"
>> somefunc print now
9-Jun-2002/8:32:33-4:00 == none When the input to somefunc is something like 'print' or any word in the rebol dictionary, we might assume that it is not a valid argument. Arguments we would be expecting would usually be a number, a name, a time, a block, etc.. So we could check for an existing known word, then perhaps it is not an argument. I don't know how to do that kind of lookup and could be slow.. It seems pretty limiting unless we are only passing in very simple strings and numbers, which in fact we often are in cgi work. But the obvious escape is just put all args in a block.
>> somefunc exists? %test.xml ;;; empty argument, keep going.. >> somefunc [exists? %test.xml] ;;; evaluate block and use as argument >> somefunc [1 "love" now ] ;;; evaluate block and use as argument >> somefunc [name "jason" email [jasonic--nomadics--org] startdir %.] ;;; name,
email are keywords arguments Which creates Python's **Kw variable keyword arguments very nicely. Except for a concise local way to embed default handling. if error? try [startdir] [startdir: "%/home/jasonic"] That works, but it's verbose next to Python. Any idiom suggestions to improve upon it within a function? Anyway feel like I just reinvented some primal rebol wheel here! Must be time to study scope and context more.. I suppose I need an 'arg-block tool I can use widely. Can anyone point me to examples using blocks as arguments, and techniques for parsing them? thanks ./Jason