Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding words to a context in REBOL

Tags:

rebol

rebol2

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?

like image 658
Gregory Higley Avatar asked Nov 04 '08 20:11

Gregory Higley


1 Answers

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
]
like image 115
Peter W A Wood Avatar answered Sep 25 '22 23:09

Peter W A Wood