[REBOL] Re: Contexts of contrived blocks
From: joel:neely:fedex at: 9-May-2001 13:51
Hi, John,
[john--schuhr--com] wrote:
> Anyone know how to explain the following?
> Whenever I create a block using 'to-block
> on a string, the words in the block have their
> own context.. what gives?
>
...
Well, as the old gentleman said, "I don't know the answer,
but I surely do admire the question!"
This seems like it might be related to another small mystery
I've been pondering on recently. Given the code:
8<------------------------------------------------------------
#!/usr/local/bin/rebol
rebol []
make object! [
scoper: func [
argvar [integer!] recur? [logic!] /local locvar
][
locvar: argvar + 1
use [usevar] [
usevar: argvar + 2
print [argvar recur? locvar usevar]
if recur? [scoper argvar + 10 false]
print [argvar recur? locvar usevar]
]
]
scoper 1 true
]
8<------------------------------------------------------------
we get the following behavior:
8<------------------------------------------------------------
>> do %scoper.r
1 true 2 3
11 false 12 13
11 false 12 13
1 true 2 13
>>
8<------------------------------------------------------------
Now, it's clear (in some sense) *WHAT* is happening. The USE
establishes a separate context in which USEVAR is known. The
single instance of that context persists across the recursive
call, therefore the value to which USEVAR is set in the inner
call is still acessible upon return to the outer call.
What is not clear to me is *HOW* and *WHEN* that takes place.
Does the USE establish that context...
1) when the source file is loaded?
2) when SCOPER is first invoked?
3) at some other time I haven't imagined?
Option (1) seems inconsistent with what I think I understand
about how FUNC and MAKE OBJECT! do (create a new context when
executed). Option (2) leaves me wondering, "How does it know
that the recursively nested call should re-use the context
created in the outer call (instead of creating another one)
and how does it find that existing context?"
It still appears that contexts have much deep magic associated
with them!
-jn-