Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: How to remove words from objects?

From: joel:neely:fedex at: 13-Jan-2004 13:54

Hi, Luke, Luke wrote:
> I dont understand Gregg what you mean when you say > the context of an object isnt dynamic... >
Once an object has been created, its "context" (namespace) is set in concrete and cannot be changed (although the values to which its words are set *can* be changed).
> But if so, then how does that explain why can I do this > (but not reverse the action): > > ;---create object > obj: make object [a: 1] > > ;---add a new word to the object > obj: make obj [b: 2] >
Because you are not adding a new word to *the*original* object, but instead are creating a *new* object (based on the original) which contains the new value. To see that this is not modifying the original, just consider this:
>> obj: make object! [a: 1] >> other: obj >> obj: make obj [b: 2] >> source obj
obj: make object! [ a: 1 b: 2 ]
>> source other
other: make object! [ a: 1 ] So the original object is still around (referent of OTHER) but now OBJ refers to a *new* object.
>> >>You can get tricky and work around it, by including a block in your >>object and using that as kind of a sub-context--because blocks are >>resizable. I think Ladislav or Joel posted a dynamic object example >>here at one time... >>
If you want shared references to an object to all show the effect of such "structural" changes, you can wrap the object in another object (i.e. create an additional level of indirection) which is the common referent:
>> wrapper: make object! [
[ inner: make object! [ [ a: 1 [ ] [ ]
>> obj: wrapper >> other: wrapper >> source obj
obj: make object! [ inner: make object! [ a: 1 ] ]
>> source other
other: make object! [ inner: make object! [ a: 1 ] ]
>> obj/inner: make obj/inner [b: 2] >> source obj
obj: make object! [ inner: make object! [ a: 1 b: 2 ] ]
>> source other
other: make object! [ inner: make object! [ a: 1 b: 2 ] ] (Of course, the wrapper could be a block as well, but using an object as a wrapper allows an easy way to put methods in the wrapper that delegate to the inner object, so you can hide the indirection from any client code.) -jn- -- Joel Neely com dot fedex at neely dot joel I had proved the hypothesis with a lovely Gedankenexperiment, but my brain was too small to contain it. -- Language Hat