[REBOL] Re: Creating functions with local variables
From: g::santilli::tiscalinet::it at: 31-Dec-2002 15:41
Hi Robert,
On Tuesday, December 31, 2002, 11:39:17 AM, you wrote:
RMM> So you still have to mention all local words.
I imagined you were looking for that; as I said, I think Ladislav
has the function for you. Anyway, here's a QAD solution:
do-local: func [code [block!] /local words stack block] [
words: clear []
stack: clear []
insert/only tail stack code
forall stack [
foreach value pick stack 1 [
if set-word? :value [insert tail words to-word :value]
if all [any-block? :value not path? :value] [
insert/only tail stack :value
]
]
]
use words code
]
>> do-local [my-var: 3 print my-var]
3
>> my-var
** Script Error: my-var has no value
** Near: my-var
>> do-local [f: func [x] [y: x * x print [x y]] f 3 do-local [y: 5 print y] print y]
3 9
5
9
>> x
** Script Error: x has no value
** Near: x
>> y
** Script Error: y has no value
** Near: y
RMM> And you have to agree on a
RMM> function that gets called because the context will be an object that
RMM> doesn't execute any code if used. You have to explicitly call a word
RMM> from the context.
No, you have not. You simply use CONTEXT instead of DO... Or, like
in the above, you simply use DO-LOCAL instead of DO.
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r