[REBOL] solved: another question about contexts and layouts
From: mh983::yahoo::com at: 12-Mar-2002 11:18
Woo hoo! I've discovered my problem (I think), and as
usual, it's pretty dumb. The following example is
essentially the same as the first, but removes some
fluff and removes 'layout from the equation.
The problem is that inner-ctx is just a block and is
not turned into an object at the time we call "context
outer-ctx".
Therefore, both obj1 and obj2 refer to the same
inner-ctx block. Since obj2 is created last, the
value bound to 'name in the inner-ctx block will be
Two
, and both obj1 and obj2 will refer to it.
The simple solution is to just include copy/deep for
the inner-ctx.
I have a hard time making sense of this concept and
would appreciate it greatly if someone has a clear
description. Is it that since the series is
identical, REBOL just points all instances to the same
memory location? For instance, in java if you write:
String x = "hello";
String y = "hello";
You are actually pointing to the same String object in
memory. But since Strings are immutable, you don't
get a surprise change to y when you change x.
I'm wondering if it's the same concept in REBOL except
the series is not immutable. Anyway, thanks for
listening. Here's the new example if anyone's
interested:
REBOL[]
outer-ctx: [
name: "unnamed"
myinner: none
inner-ctx: [
emit: does [print ["name is" name]]
]
init: does [
myinner: context inner-ctx
]
]
; create object1 and give it a name
obj1: context outer-ctx
obj1/name: "One"
; create object2 and give it name
obj2: context outer-ctx
obj2/name: "Two"
obj1/init
print obj1/name ; prints "One"
obj1/myinner/emit ; print "Two"