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

[REBOL] Re: How to check function arguments ?

From: g:santilli:tiscalinet:it at: 9-Jun-2002 16:12

Hi Jason, On Sunday, June 09, 2002, 3:21:28 PM, you wrote: JC> When the input to somefunc is something like 'print' or any word in the rebol JC> dictionary, we might assume that it is not a valid argument. Arguments we would JC> be expecting would usually be a number, a name, a time, a block, etc.. So we JC> could check for an existing known word, then perhaps it is not an argument. I JC> don't know how to do that kind of lookup and could be slow.. It seems pretty JC> limiting unless we are only passing in very simple strings and numbers, which in JC> fact we often are in cgi work. I don't think that what you want to do can be done, at least easily (even if you get the word 'PRINT, and decide to leave it alone, how do you tell REBOL to evaluate it AFTER your function?). REBOL does not support variable number of arguments because of its freeform syntax. If you like parens, then you can workaround the problem with variable args with them:
>> (somefunc) print "done"
no value passed done JC> Which creates Python's **Kw variable keyword arguments very nicely. Yes, and you can even create your own dialect, which can make things even better. JC> Except for a concise local way to embed default handling. See my earlier post. :) Of course it needs to be modified if you want to use it with varargs,
>> get-or: func [word default] [either value? word [get word] [default]] >> somefunc: func [inp [any-type!]] [inp: get-or 'inp "default value" print inp] >> somefunc
default value
>> somefunc 1
1 or any variation,
>> set-default: func [word default] [if not value? word [set word default] word] >> get-or2: func [word default] [any [if value? word [word] default]]
JC> Anyway feel like I just reinvented some primal rebol wheel here! Must be time to JC> study scope and context more.. I suppose I need an 'arg-block tool I can use JC> widely. I still think you'd better go with refinements, or just pass NONE to mean "default"; if your function is complex enough to make this a problem, then pass a single block and PARSE it (i.e. make your own dialect :). JC> Can anyone point me to examples using blocks as arguments, and techniques for JC> parsing them? LAYOUT? ;-) Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r