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

[REBOL] Re: inserting new properties into existing objects

From: ingo:2b1 at: 22-Nov-2002 7:57

Hi Brett, Brett Handley wrote:
> Hi James, > >> My question is this, apart from cloning+extending is there a >> faster/more efficient way to make new properties and assign them >> values?
<...>
> As I understand it objects in REBOL are a collection of words, each > word standing in as a symbol to represent the data, not being or > containing the data itself.
I'm sorry, but you aren't right here.
>> a: make object! [b: 1 c: "That's a test" d: func[a][print a] e: make
object! [f: 1 g: "Another test" h: func[x][print join x x]]]
>> b: make a [] >> b/b: 3
== 3
>> b/c: "changed string"
== "changed string"
>> b/d: func[a][print a * a] >> b/e/f: 7
== 7
>> b/e/g: "second changed string"
== "second changed string"
>> b/e/h: func[y][print "changed func"] >> probe a
make object! [ b: 1 c: "That's a test" d: func [a][print a] e: make object! [ f: 7 g: "second changed string" h: func [y][print "changed func"] ] ]
>> probe b
make object! [ b: 3 c: "changed string" d: func [a][print a * a] e: make object! [ f: 7 g: "second changed string" h: func [y][print "changed func"] ] ] As you can see, only the embedded object is shared among the the objects, everything else is cloned. Coming back to the original question, if extending objects is very common in your design, you might think about using bare blocks: food: func [f-food [string!]][ compose/deep [ foodname (f-food) ] ] fish: func [f-grade [string!] f-species [string!] f-foods [block!] /local ftmp][ compose/deep [ size (f-grade) species (f-species) favourite-foods [ ( ftmp: copy [] repeat f f-foods [ insert/only tail ftmp food f ] ftmp ) ] ] ] onefish: fish "X-Large" "Mackeral" ["krill" "smaller fish"] twofish: fish "Large" "Cod" ["slow fish"] redfish: fish "Medium" "Snapper" ["blue cod"] bluefish: fish "Small" "Bluefin Tuna" ["fish"] probe onefish ;purpose to add 2 new properties to onefish insert tail onefish [area "5a" taste "A-Grade"] probe onefish insert tail onefish/favourite-foods/1 [decay-time "forever"] probe onefish onefish/size onefish/area The downside: access to block! elements is slower than to object! elements. Kind regards, Ingo