[REBOL] Re: Object private member (was: objects: overhead, private data, naming
From: rotenca:telvia:it at: 20-Sep-2001 20:25
Hi, Joel
> No. The use of SET affects the global context, rather than
> the context of the object you're constructing.
No. It affects the context to which the word is dinamically binded, in this
example a Use block:
>> a: 1 use [a] [x: context [b: 2 set 'a 3] print a] print a
3
1
in the context in which 'set is called, "a" is binded to the use block, so
'set affect the use-binded 'a:
In the next example 'set affect the 'a binded to the object 'x and do not
change the global 'a or the use 'a:
>> a: 1
== 1
>> use [a] [a: 2 x: context [a: 3 set 'a 4 ] print ["Use:" a]]
Use: 2
>> print ["Global:" a "Object:" x/a]
Global: 1 Object: 4
To explain, lets take this simple example:
context [a: 3]
At the start Rebol creates a new context with the word 'a unset, while self
points to the context:
>> context [print mold first get 'self a: 3]
[self a]
>> context [print [a] a: 3]
?unset?
>> context [print mold third self a: 3]
[a:]
It is like if the code would be:
context compose [a: (())]
Now Rebol execute the block: [a: 3] and it finds:
a: 3
and bind 'a to the Actual context: at that time, the Actual context says that
the 'a is local to the object 'x, so x/a is set and becomes = 3. Which is the
result we are waiting.
Now get this example:
context [a: 3 set 'a 4 ]
After all the pass we have already seen we have:
set 'a 4
Rebol binds 'a to the Actual context. In this case:
'set -> global
'a -> object
then executes the code which changes the value of x/a not of global 'a.
In this new example:
>> a: 1 context [set 'a 4 print a a: 3 print a] a
4
3
== 1
Rebol first unset x/a, then set it at 4 then set it at 3, while the global 'a
remain unchanged.
So, in the block the word argument of set is always bound to the object, only
if the word is not defined in the object, the bind default to the external
context of the object:
context [set 'b 4 a: 3] ; b is global because 'b is NOT defined in the object
use [b][context [set 'b 4 a: 3]] ; b is local at Use block because 'b is NOT
defined in the object
use [b][context [set 'b 4 b: 3]] ;b is local at object, because 'b IS defined
in the object
> -jn-
-----
ciao
romano