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

[REBOL] Re: How to check function arguments ?

From: ingo:2b1 at: 9-Jun-2002 20:13

Hi Jason, Jason Cunliffe wrote:
> 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.
Well, trouble is, some-func never gets to see 'print, let's walk through it:
>> somefunc print "hello"
The interpreter finds 'somefunc, and sees that it's a function which would be happy to get an argument (in our example it would be equally happy without one, but that will only be checked _after_ nothing at all has been found). Now it stumbles on 'print "ahh, that's word, now let's find the value of that word". The value happens to be a function, this function wants one argument, too, so it eats the string "hello", does its work, and the return value of print (which is nothing at all, by the way), is given to somefunc as an argument. You could change somefunc like this
>>somefunc: func['inp[any-type!]][if value? 'inp [print inp "do stuff"]]
(Notice the 'tick in front of inp) Now you'll get:
>> somefunc print "hello"
print == "hello" -> somefunc eats the _word_ 'print, without evaluating it, and "hello" is just returned, because there's nothing left to do with it. It is possible to do the print command now, but the problem is to get to prints argument. And now for something completely different. Your real problem is, that Rebol functions just try to get all their arguments, and there seems to be no way to tell a function where to stop searching, while most other languages have a means for that, be it semicolons, parents, or what not. Now Rebol has some Lisp ancestry, so you can always use those lispy parens:
>> (somefunc)
== none
>> (somefunc) print "hello"
hello <..>
> But the obvious escape is just put all args in a block.
Well, that's the most rebolious way, I think. I hope that shines a little light on the subject, Ingo