Dynamically adding words to a context in REBOL
Asked Answered
C

3

7

Imagine the following REBOL code:

foo: context [bar: 3]

I now have a context foo in which 'bar is defined. How can I dynamically inject a new word into this context? Is it possible?

I've tried:

set/any in foo 'baz 3

But that doesn't work because the expression in foo 'baz fails because there is no word 'baz defined in the foo context.

I should add that I realize one way to do this is as follows:

foo-prototype: [bar: 3]
foo: context foo-prototype
foo: context head append foo-prototype [baz: 3]

But what if you don't have access to foo's prototype block?

Ceramics answered 4/11, 2008 at 20:53 Comment(0)
A
8

You can achieve the same by using the existing object as a prototype to create a new object.

>> foo: make object! [bar: 3]
>> foo: make foo [baz: 3]
>> probe foo
make object! [
    bar: 3
    baz: 3
]
Accumulative answered 10/1, 2009 at 10:48 Comment(1)
This is one of the fundamental aspects of REBOL, but at the time I asked this question, I wasn't aware you could do this.Ceramics
C
4

There are several ways to work around the fact that in REBOL/2 it's just not posssible to extend object contexts.

Probably you can just use BLOCK!s instead of OBJECT!s:

>> blobject: [foo 1]
== [foo 1]
>> blobject/bar
** Script Error: Invalid path value: bar
** Near: blobject/bar
>> append blobject [bar 2]
== [foo 1 bar 2]
>> blobject/bar: 3
== 3

You can even make 'self working by just appending the object itself:

>> insert blobject reduce ['self blobject]
== [[...] foo 1 bar 2]
>> same? blobject blobject/self
== true

But as you've asked for extending OBJECT!s, you may go for Peter W A Wood's solution to simply clone the object. Just keep in mind that with this approach the resulting clone really is a different thing than the original object.

So, if some word has been set to hold the object prior to cloning/extending, after cloning the object that word will still hold the unextended object:

>> remember: object: make object! [foo: 1]
>> object: make object [bar: 2]
>> same? remember object
== false
>> probe remember
make object! [
   foo: 1
]

In case it's essential for you to keep "references" to the object intact, you might want to wrap the object you need to extend in an outer object as in

>> remember: object: make object! [access: make object! [foo: 1]]
>> object/access: make object/access [bar: 2]
>> same? remember object
== true

You can then safley extend the object while keeping, given you only store references to the container.

REBOL/3, btw, will allow adding words to OBJECT!s.

Custodian answered 10/1, 2009 at 19:41 Comment(1)
Very informative answer! I've learned a TON about REBOL since I posted this question.Ceramics
W
1

Said in REBOL/Core User Guide: "Many blocks contain other blocks and strings. When such a block is copied, its sub-series are not copied. The sub-series are referred to directly and are the same series data as the original block."

Wastebasket answered 18/11, 2010 at 7:6 Comment(1)
Correct me if I'm wrong, but doesn't this relate to object!s instead of blocks!s?Kneehigh

© 2022 - 2024 — McMap. All rights reserved.