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?
You can achieve the same by using the existing object as a prototype to create a new object.
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."
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:
You can even make 'self working by just appending the object itself:
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:
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
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.