Nested objects
[1/3] from: gavin::mckenzie::sympatico::ca at: 27-Jun-2001 7:40
Hi,
I'm not sure how to do this...
Say, I've got a bunch of nested objects that I setup conveniently using
syntax like:
obj: make object! [
foo: 1
bar: 2
nested-object:
make object! [
abc: 3
xyz: 4
]
]
Now, I want all of my inner objects to have a property that ties them to
their parent objects. I went looking to see if there was such a built-in
property, but the only automatic property of objects appears to be the word
'self'.
So, I know that I can easily enough write a routine that walks my tree of
nested objects and attempts to wire them together, but I was hoping that
there was a way I could get it to happen either:
a) automatically
b) with a little extra work when I define the objects; something like:
obj: make object! [
foo: 1
bar: 2
nested-object:
make object! [
parent: obj
abc: 3
xyz: 4
]
]
That approach seemed promising, except that it seems that parent doesn't
hold a reference to the same 'obj' object, rather it holds a copy. My
testing shows that if I change obj/foo to another value, that
obj/nested-object/parent/foo hasn't changed value.
Any thoughts?
Maybe I'm missing something rather straightforward.
Regards,
Gavin.
[2/3] from: agem:crosswinds at: 27-Jun-2001 14:53
RE: [REBOL] Nested objects
[gavin--mckenzie--sympatico--ca] wrote:
> Hi,
> I'm not sure how to do this...
<<quoted lines omitted: 13>>
> property, but the only automatic property of objects appears to be the word
> 'self'.
if you want this only for new objects,
obj-class: [
make object![..]
]
new-object: do obj-class
sub-class: append copy obj-class[extension]
if you want to make object, change vars and make from that with
inner objects rebound, forget it..
> So, I know that I can easily enough write a routine that walks my tree of
> nested objects and attempts to wire them together, but I was hoping that
<<quoted lines omitted: 15>>
> testing shows that if I change obj/foo to another value, that
> obj/nested-object/parent/foo hasn't changed value.
your execution order:
1) obj: something
2) nested-object/parent: obj
3) obj: new object with parent-object..
make object![
set 'obj self
..
]
sets 'obj at first
[3/3] from: joel:neely:fedex at: 27-Jun-2001 3:31
Hi, Gavin,
Gavin F. McKenzie
wrote:
> Say, I've got a bunch of nested objects that I setup
> conveniently using syntax like:
<<quoted lines omitted: 7>>
> ]
> ]
Flo: "Doctor, it hurts when I do this!"
Moe: "Well, then don't do that!"
Seriously, we had a long thread several weeks back on the
topic of "nested objects". This quickly gets into some VERY
subtle (IMHO) aspects of REBOL behavior.
> Now, I want all of my inner objects to have a property that
> ties them to their parent objects...
>
The only way I know how to make sense of the terms "parent
object" and "child object" for code like your sample is the
issue of which context(s) was/were used to resolve words that
were evaluated during the construction of NESTED-OBJECT.
> Maybe I'm missing something rather straightforward.
>
Not to my knowledge. Given
>> obj1: make object! [foo: 1 child: none]
>> obj2: make obj1 [foo: 2]
>> obj3: make object! [baz: 42 parent: none]
>> obj1/child: obj3
>> obj2/child: obj3
>> obj3/baz: 17
== 17
>> source obj1
obj1:
make object! [
foo: 1
child:
make object! [
baz: 17
parent: none
]
]
>> source obj2
obj2:
make object! [
foo: 2
child:
make object! [
baz: 17
parent: none
]
]
I think that tree-walking (or other relationship-management
at run time) is the best way to determine whether
obj3/parent: obj1
or
obj3/parent: obj2
is what you need at any given point.
-jn-
------------------------------------------------------------
Programming languages: compact, powerful, simple ...
Pick any two!
joel'dot'neely'at'fedex'dot'com
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted