[REBOL] Re: A REBOL challenge - The Information World
From: larry:ecotope at: 7-Dec-2001 12:19
Hi Gregg,
> Regarding use of 'protect: Can you protect an entire context, or the words
> *in* a context? I haven't figured out how to do that yet.
>
PROTECT basically adds a protected flag to a word value. A REBOL value of
datatype word includes a pointer to its context. The use of PROTECT will
protect the word in that context. The argument to PROTECT can be either a
word or a block of words.
For contexts created by the function USE, which creates a local context for
execution of a block of code, the use of PROTECT is straightforward:
>> use [a] [a: 1 protect 'a loc-a: does ['a]]
We have created a local context with the word 'a' being the only local
variable. In the code block we create a global function LOC-A which returns
the local word 'a', so that we can see what's happening. The word 'a' is
both private and (after the PROTECT 'a) protected.
>> a: 52
== 52
The global word 'a' is set to 52.
>> loc-a
== a
>> get loc-a
== 1
We can access the value of the local 'a'.
But it is protected from change.
>> set loc-a 2
** Script Error: Word a is protected, cannot modify
** Near: set loc-a 2
But because we have the word, we can use the UNPROTECT function on it.
>> unprotect loc-a
>> set loc-a 2
== 2
>> get loc-a
== 2
For local contexts created by MAKE FUNCTION! and MAKE OBJECT! the situation
is more complex and the usefulness of PROTECT is limited.
BTW In future, with REBOL/Core 3.0, we have been told that REBOL will have
modules, which should alleviate most of the currently existing namespace
issues.
-Larry