[REBOL] Rebol Tech, please answer. Two questions. Re:(2)
From: jeff:rebol at: 12-Sep-2000 7:17
Howdy Bob:
> >Always creating a new object from the old one when I want
> >to add a refinement doesn't appeal to me.
>
> it bugs me too, further it makes incorporation of that
> object into some other(s) a chore to track if it is linked
> in multiple places.
>
> does anyone have an elegant/quick way to see who refers to
> an object? (IE who else has an == link to the same self'
> value?)
Here's a way:
Every object has a block of referring objects and referred
to objects. When ever you link to an object, you add
yourself to that object's referring block. When ever you
unlink, you remove yourself from their referring block.
Etc..
a: make object! [referring: copy [] points-to: copy []]
b: make object! [
referring: none points-to: reduce ['a a]
append a/referring reduce ['b self]
]
link-obj: func ['from 'to /obj1 /obj2][
obj1: get from
obj2: get to
append obj2/referring reduce [from obj1]
append obj1/points-to reduce [to obj2]
]
unlink-obj: func ['from 'to][
remove/part find obj2/referring from 2
remove/part find obj1/points-to to 2
]
Now you can:
b/points-to/a/referring/b/points-to/a
etc...
So we're making a graph? Maybe start a "how do I represent
graphs in REBOL" thread? :)
By the way: The way we used the two blocks above is a
general mechanism for easily adding and removing refinements
to an object:
foo: make object! [
a:
b: none
x-refs: copy []
]
Let's add 'c:
append foo/x-refs [c 99] ;-- can also bind things into foo here
foo/x-refs/c
Removal is similarly easy:
remove/part find foo/x-refs 'c 2
Blocks, the universal dynamic container, are best for things
where you want to add and remove from frequently. In REBOL,
objects are less dynamic than blocks. Use them together and
make the world a better place.
-jeff