[REBOL] Re: Function Context Query
From: joel:neely:fedex at: 3-Dec-2001 10:42
Hi, Mark,
[Robbo1Mark--aol--com] wrote:
> Surely this is incorrect?
>
> >> a: has [b c] [ print b print c]
> >> a
> none
> none
> >> a/local 3 4
> 3
> 4
>
> Surely local words default value should be unset
> until they are defined?
>
> Also in the second case is it correct to be
> able to pass values to local words in this manner?
>
I think you answered your own question; adding the LOCAL
refinement appears to invite A to set its locals as if
they were ordinary "optional" refinement args. As
additional evidence, note this
>> a: has [b c] [ print b print c]
>> source a
a: func [/local b c][print b print c]
>> a
none
none
>> a/local 17
** Script Error: a is missing its c argument
** Where: halt-view
** Near: a/local 17
>>
>
> How is the above different from this....
>
> >> use [b c] [print b print c]
> ** Script Error: b has no value
> ** Near: print b print c
>
> Can somebody please explain the wisdom of this
> or is it a bug?
>
Neither wisdom nor bug, just a different mechanism;
USE creates a context containing the word(s) from its
first argument, then evaluates the expression(s) from
its second argument in that new context. Since your
sample didn't do anything to initialize B and C in that
new context, they had no values.
(Actually, USE also has a side effect -- it modifies its
second argument so that words named in the first argument
are bound to the new context. That explains the following
bit of behavior:
>> thing: [print b print c]
== [print b print c]
>> b: "Hello, "
== "Hello, "
>> c: "world!"
== "world!"
>> do thing
Hello,
world!
Presumably there's no surprise there.
>> use [b c] [
[ b: 1
[ c: 2
[ do thing
[ ]
Hello,
world!
If there's surprise here, recall that only occurrences of B
and C *within* the second argument of USE are re-bound to the
new context. Therefore THING still contains the global B and
C words.
>> use [b c] thing
** Script Error: b has no value
** Where: halt-view
** Near: print b print c
No surprise here, because the new B and C never got initiliized.
>> do thing
** Script Error: b has no value
** Where: halt-view
** Near: print b print c
If there's surprise here, recall that USE rebound occurrences
of B and C within THING to the new context. Therefore, THING
no longer contains references to the global B and C.
HTH!
-jn-
--
This sentence contradicts itself -- no actually it doesn't.
-- Doug Hofstadter
joel<dot>neely<at>fedex<dot>com