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

How do I dynamically modify an object?

 [1/11] from: edanaii:cox at: 14-Jan-2003 13:36


Using the examples from the REBOL On-line dictionary:
>> Example: Make Object! [ Name: "Fred" Age: 5000000 ] >> Get in Example 'Name
== "Fred"
>> Set in Example 'Name "Fred Flintstone"
== "Fred Flintstone" How do I dynamically add to example, after it has been created, Address: 123 Bedrock Path . I tried using 'Set, 'Make and a couple other functions, but they didn't work. I was probably using them wrong. :) -- Sincerely, | For long you live and high you fly. Ed Dana | And smiles you'll give and tears you'll cry Software Developer | And all you touch and all you see, 1Ghz Athlon Amiga | Is all your life will ever be. | -- Pink Floyd, Breathe.

 [2/11] from: greggirwin:mindspring at: 14-Jan-2003 14:19


Hi Ed, ED> Using the examples from the REBOL On-line dictionary: ED> >> Example: Make Object! [ Name: "Fred" Age: 5000000 ] ED> >> Get in Example 'Name ED> == "Fred" ED> >> Set in Example 'Name "Fred Flintstone" ED> == "Fred Flintstone" example/name: "Fred Flintstone" ED> How do I dynamically add to example, after it has been created, Address: ED> "123 Bedrock Path". ED> I tried using 'Set, 'Make and a couple other functions, but they didn't ED> work. I was probably using them wrong. :) Just use your object as a prototype with MAKE and put your new field in the spec block. example: make example [address: "123 bedrock path"] make object! [ Name: "Fred Flintstone" Age: 5000000 address: "123 bedrock path" ] -- Gregg

 [3/11] from: tim::johnsons-web::com at: 14-Jan-2003 12:31


* Ed Dana <[EDanaII--Cox--net]> [030114 11:50]:
> Using the examples from the REBOL On-line dictionary: > >> Example: Make Object! [ Name: "Fred" Age: 5000000 ]
<<quoted lines omitted: 6>>
> I tried using 'Set, 'Make and a couple other functions, but they didn't > work. I was probably using them wrong. :)
Hi Ed: Below are a couple of examples. I'm also hoping others will chime in about the subtle differences in 'get and 'set, as they still elude me also. =t
>> t1: make example[address: "here"] >> probe t1
make object! [ Name: "Fred" Age: 5000000 address: "here" ]
>> append t1/address " there"
== "here there"
>> probe t1
make object! [ Name: "Fred" Age: 5000000 address: "here there" ]
>> t1/address: "Anywhere"
== "Anywhere"
>> probe t1
make object! [ Name: "Fred" Age: 5000000 address: "Anywhere" ]
> -- > Sincerely, | For long you live and high you fly.
<<quoted lines omitted: 7>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.alaska-internet-solutions.com http://www.johnsons-web.com

 [4/11] from: joel:neely:fedex at: 14-Jan-2003 15:09


Hi, Ed, The short answer is, "You can't!" Ed Dana wrote:
> Using the examples from the REBOL On-line dictionary: > >> Example: Make Object! [ Name: "Fred" Age: 5000000 ]
<<quoted lines omitted: 6>>
> I tried using 'Set, 'Make and a couple other functions, but they > didn't work. I was probably using them wrong. :)
In previous discussions, the following approach has been taken:
>> example: make object! [name: "Fred" age: 5000000] >> example/name
== "Fred"
>> example/name: "Barney"
== "Barney"
>> source example
example: make object! [ name: "Barney" age: 5000000 ]
>> example: make example [address: "123 Bedrock Path"] >> source example
example: make object! [ name: "Barney" age: 5000000 address: "123 Bedrock Path" ] But the obvious flaw is that EXAMPLE has now been set to a *new* object constructed from the original and the expanded spec block. That didn't change the original object at all. REBOL doesn't have AFAICT any way to expand the context of an object (or function) after the original definition. To show that the original did not change, just capture a spare reference to it, as below:
>> otherref: example: make object! [name: "Fred" age: 5000000]
and then evaluate the same expressions as above:
>> example/name
== "Fred"
>> example/name: "Barney"
== "Barney"
>> example: make example [address: "123 Bedrock Path"] >> source example
example: make object! [ name: "Barney" age: 5000000 address: "123 Bedrock Path" ] then look at the spare reference:
>> source otherref
otherref: make object! [ name: "Barney" age: 5000000 ] Just to prove that ADDRESS is only valid for the *new* object...
>> otherref/address
** Script Error: Invalid path value: address ** Where: halt-view ** Near: otherref/address
>> example/address
== "123 Bedrock Path" Some other languages (including Perl and Python) allow new attributes to be added to an existing object after initial creation is long past. While this does open the (another ;-) door to mis-use via the creation of values that no longer resemble their original source code, it is certainly an interesting concept for a dynamic language (and one can use reflection/introspection to manage/follow such changes). -jn- -- ---------------------------------------------------------------------- Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446

 [5/11] from: al:bri:xtra at: 15-Jan-2003 10:24


Ed wrote:
> Using the examples from the REBOL On-line dictionary: > >> Example: Make Object! [ Name: "Fred" Age: 5000000 ]
<<quoted lines omitted: 3>>
> == "Fred Flintstone" > How do I dynamically add to example, after it has been created, Address:
123 Bedrock Path .
> I tried using 'Set, 'Make and a couple other functions, but they didn't
work. I was probably using them wrong. :) Rebol unfortunately doesn't allow objects to be expanded. However you can expand block! values. For example:
>> Example: [ Name "Fred" Age 5000000 ]
== [Name "Fred" Age 5000000]
>> repend Example ['Address "123 Bedrock Path"]
== [Name "Fred" Age 5000000 Address "123 Bedrock Path"]
>> example/name
== "Fred"
>> example/address
== "123 Bedrock Path" Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [6/11] from: edanaii:cox at: 14-Jan-2003 15:41


Joel Neely wrote:
>Hi, Ed, >The short answer is, "You can't!"
<<quoted lines omitted: 58>>
>certainly an interesting concept for a dynamic language (and one can >use reflection/introspection to manage/follow such changes).
That's pretty much what I thought. Admittedly, such abilities could get out of control and corrupt your object. I just wanted to see if REBOL could do it. Thanks Joe. -- Sincerely, | Nearly all men can stand adversity, but if you Ed Dana | want to test a man's character, give him power. Software Developer | -- Abraham Lincoln 1Ghz Athlon Amiga |

 [7/11] from: edanaii:cox at: 14-Jan-2003 15:46


Andrew Martin wrote:
>Ed wrote: >>Using the examples from the REBOL On-line dictionary:
<<quoted lines omitted: 30>>
>>> >== "123 Bedrock Path"
That's what finally I realized. My final solution is to do this:
>> x: {Address: "Bedrock Central" Phone: "Just shout loudly!" }
== {Address: "Bedrock Central" Phone: "Just shout loudly!" }
>> Contact: Make Example to-block x >> Probe Contact
make object! [ Name: "Fred Flintstone" Age: 35000000 Address: "Bedrock Central" Phone: "Just shout loudly!" ] That certainly gets the job done... -- Sincerely, | The Traveller awaits the morning tide. He doesn't Ed Dana | know what's on the other side. But something deep Software Developer | inside of him keeps telling him to go. He hasn't 1Ghz Athlon Amiga | found a reason to say no. | -- the Alan Parsons Project, Days are Numbers. =========== http://OurWorld.CompuServe.com/Homepages/EDanaII ===========

 [8/11] from: edanaii:cox at: 14-Jan-2003 15:34


Gregg Irwin wrote:
>Hi Ed, >ED> Using the examples from the REBOL On-line dictionary:
<<quoted lines omitted: 16>>
> address: "123 bedrock path" >]
I know how to do that. That is not what I was asking. :) How do I (Can I?) add Address to an _existing_ object *after* that object has been created. I'm assuming it's not possible... And why would I want to do this? Why not? =) -- Sincerely, | Tell me, tell me, where I'm going; I don't know Ed Dana | where I've been. Tell me, tell me. Oh won't you Software Developer | tell me, and then tell me again! My body's aching, 1Ghz Athlon Amiga | my heart is breaking and I don't know where to go! | So tell me, tell me, why don't you tell me? I just | gotta know! - Styx, Crystal Ball

 [9/11] from: james:mustard at: 15-Jan-2003 11:50


Umm.. normally I will just do this: fred: make object! [ Name: "Fred" Age: 5000000 ] fred: make fred [ Address: "123 Bedrock Path" ] providing of course you wish to permanently modify fred. Alternatively you could use a function to preserve your base caveman class (maybe you want a Barney?): make_caveman: func [cv_name cv_age][ make object! [ Name: cv_name Age: cv_age ] ] fred: make_caveman "Fred" 5000000 fred: make fred [ Address: "123 Bedrock Path" ] James.

 [10/11] from: joel:neely:fedex at: 14-Jan-2003 17:35


Hi, Ed, At the risk of offending some Perlophobes among us... ;-) Ed Dana wrote:
> How do I (Can I?) add Address to an _existing_ object *after* that > object has been created. >
One way of managing an object's namespace in Perl is to store the attributes explicitly within a key/value structure (hash, in Perl). We can pull the same trick here, at the cost of a little explicit machinery to manage that structure, as follows: example: make object! [ name: "Fred" age: 5000000 dynamic: [] dynamic-get: func ['key [word!]] [select dynamic key] dynamic-set: func ['key [word!] val [any-type!] /local where] [ either found? where: find dynamic key [ change next where val ][ append/only append dynamic key val ] val ] ] So that EXAMPLE now has "static" attributes, such as NAME or AGE, and a collection of "dynamic" attributes. First the static ones:
>> example/name
== "Fred"
>> example/age
== 5000000 Then let's add a dynamic one (after checking to make sure that it is not already there).
>> example/dynamic-get address
== none
>> example/dynamic-set address "123 Bedrock Way"
== "123 Bedrock Way"
>> example/dynamic-get address
== "123 Bedrock Way" Once a dynamic attribute has been added via DYNAMIC-SET we can use the standard path notation to retrieve...
>> example/dynamic/address
== "123 Bedrock Way" ... or even to redefine it!
>> example/dynamic/address: "456 Slag Street"
== [address "456 Slag Street"]
>> example/dynamic-get address
== "456 Slag Street"
>> source example
example: make object! [ name: "Fred" age: 5000000 dynamic: [address "456 Slag Street"] dynamic-get: func ['key [word!]][select dynamic key] dynamic-set: func ['key [word!] val [any-type!] /local where][ either found? where: find dynamic key [ change next where val ] [ append/only append dynamic key val ] val ] ]
>> example/dynamic/address
== "456 Slag Street" That's not exactly what you asked for, but it's at least a distant cousin.
> And why would I want to do this? Why not? =) >
Just because! After all, one would expect a dynamic "object language" to be very flexible in what one can do with objects, right! ;-) -jn-

 [11/11] from: edanaii:cox at: 15-Jan-2003 8:08


Joel Neely wrote:
>That's not exactly what you asked for, but it's at least a distant >cousin. >
But it gets the job done...
>>And why would I want to do this? Why not? =) >>
<<quoted lines omitted: 3>>
>After all, one would expect a dynamic "object language" to be very >flexible in what one can do with objects, right! ;-)
Precisely. :) That's why I tried it, just to see if REBOL was flexible enough to do it. -- Sincerely, | Ed Dana | Faith is believing what you know ain't so. Software Developer | -- Mark Twain 1Ghz Athlon Amiga |

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted