Question

This question is about dynamically adding code to contexts or objects in rebol 2, it is related to question Dynamically adding words to a context in REBOL but it is not the same kind.

If I want to dynamically add code to a rebol objects using its code block I got in trouble due to weird behaviour:

>> append third o [c: 3]
== [a: 1 b: 2 c: 3]

but...

>> first o
== [self a b]
>> second o
== [make object! [
        a: 1
        b: 2
    ] 1 2]
>> third o
== [a: 1 b: 2]

the append is missing! same if appending to first o or second o

this do not occur using "common" blocks:

>> m: [a [b] c]
== [a [b] c]
>> append m 3
== [a [b] c 3]
>> m
== [a [b] c 3]
>> append second m 1
== [b 1]
>> m
== [a [b 1] c 3]

why is this?

Was it helpful?

Solution

The third function doesn't return the original object spec, it returns a new block of set-words and values that are generated from the words and values of the object. So you are appending to that new block (which affects the block but not the object).

You can't get a series reference to the original block passed in as the object spec. It's thrown away after the object is constructed, and it has no further effect on the object anyway. So even if you had saved a reference to the block you passed to make object, appending to it wouldn't do anything to the object either.

You simply can't append to objects in Rebol 2. But in Rebol 3, you can append to objects:

>> append make object! [a: 1 b: 2] [c: 3]
== make object! [
    a: 1
    b: 2
    c: 3
]

(Note: Don't use the ordinal reflectors in Rebol 2. Use words-of instead of first, values-of instead of second, and body-of instead of third. The ordinal reflectors have been deprecated since Rebol 2.7.7, which came out 5 years ago, and have been removed in Rebol 3.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top