[REBOL] Re: Some other questions
From: joel:neely:fedex at: 23-Nov-2003 1:27
Hi, Mike,
Here we jump off the deep end... ;-)
Mike Loolard wrote:
> 5) On the webpage about objects one passage mentioned that "sub-objects"
> don't get cloned-
> what does that mean ? Reading that I would expect that all objects within an
> object aren't
> available when I create an object on the basis of that object.
> But I seem to be able to use it anyway. Maybe I am getting something wrong
> here ?
>
If you have an object like this:
proto: make object! [
ID: 1
label: "My object"
]
then whenever you say
instance-1: make proto []
REBOL will implicitly clone/copy the string value for the LABEL
attribute, so that the new object doesn't have the *same* string
as the prototype.
Aside on Same-ness:
Consider this transcript:
>> a: "Whoopee!"
== "Whoopee!"
>> b: a
== "Whoopee!"
>> a/8: #"?"
== "Whoopee?"
>> b
== "Whoopee?"
The reason that the value for B changed is that B was set to (refer
to) the *same* string as A (i.e. not a copy):
>> a = b
== true
>> same? a b
== true
therefore there's only one string, but A and B can both "get at" it.
On the other hand, if we explicitly ask for copying:
>> a: "Whoopee!"
== "Whoopee!"
>> b: copy a
== "Whoopee!"
>> a = b
== true
>> same? a b
== false
>> a/8: #"?"
== "Whoopee?"
>> b
== "Whoopee!"
A similar situation (equal but not same) would result from saying
>> a: "Whoopee!"
== "Whoopee!"
>> b: "Whoopee!"
== "Whoopee!"
It's important to remember that "same" and "equal" aren't the same!
End of Aside on Same-ness
That means that we can examine our objects:
>> ? proto
PROTO is an object of value:
make object! [
ID: 1
label: "My object"
]
>> ? instance-1
INSTANCE-1 is an object of value:
make object! [
ID: 1
label: "My object"
]
>> same? proto/label instance-1/label
== false
and see that the LABEL attributes are equal strings, but are not the
same string.
Now, suppose that our prototype had an object as the value of one of
its attributes instead:
proto: make object! [
ID: 1
label: make object! [
part-nr: 38562
name: "Flanged widget"
]
]
and we used it as the basis of a new instance:
instance-1: make proto []
Now we can examine our objects
>> ? proto
PROTO is an object of value:
make object! [
ID: 1
label:
make object! [
part-nr: 38562
name: "Flanged widget"
]
]
>> ? instance-1
INSTANCE-1 is an object of value:
make object! [
ID: 1
label:
make object! [
part-nr: 38562
name: "Flanged widget"
]
]
>> same? proto/label instance-1/label
== true
and see that the "sub-object" referred to be the LABEL attribute
of INSTANCE-1 is the *same* object that is referred to by the LABEL
attribute of PROTO. That means (again) that changes/mutations of
that "sub-object" will be seen everywhere that it is referenced.
>> replace proto/label/name "widget" "blivet"
== "Flanged blivet"
>> ? instance-1
INSTANCE-1 is an object of value:
make object! [
ID: 1
label:
make object! [
part-nr: 38562
name: "Flanged blivet"
]
]
Now, to sum all of that up in a single sentence:
When a new object is created from a prototype object,
series-valued attributes of the prototype are copied,
but object-valued attributes are shared.
Hope this helps!
-jn-