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

inserting new properties into existing objects

 [1/5] from: james::mustard::co::nz at: 22-Nov-2002 12:04


In the following example below the intention is to create a series of base fish objects and then extend one of them in the most efficient possible way. I have used a function for the base class and also a food class. After this I have modified onefish by reassigning it a modified clone of itself. Something similar was done to onefish's first food source. My question is this, apart from cloning+extending is there a faster/more efficient way to make new properties and assign them values? (especially if I have a 1024x768 image of each fish stored with data..) food: func [f-food [string!]][ make object! copy [ foodname: f-food ] ] fish: func [f-grade [string!] f-species [string!] f-foods [block!]][ make object! copy/deep [ size: f-grade species: f-species favourite-foods: copy [] foreach f f-foods [ append favourite-foods food f ] ] ] 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 onefish: make onefish [area: "5a" taste: "A-Grade"] probe onefish onefish/favourite-foods/1: make onefish/favourite-foods/1 [decay-time: forever ] probe onefish halt

 [2/5] from: brett:codeconscious at: 22-Nov-2002 12:02


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?
I think that is the only way - and it should be very fast.
> (especially if I have a 1024x768 image of each fish stored with data..)
Here's an analogy. Take a photo of the building you live in, get it rinted - now would you be worried about whether you could lift the print off the table because the building is very big? 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. Regards, Brett.

 [3/5] 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

 [4/5] from: brett:codeconscious at: 23-Nov-2002 0:02


Hi Ingo,
> > 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.
Thank you for the correction. Hmm looks like I have to restart my REBOL education! Regards, Brett.

 [5/5] from: james:mustard at: 23-Nov-2002 12:21


Thanks for the clarification Ingo, I'll have to have a bit of a think about the object vs block setup as I would like to make extendable data objects but retain fast access. I was a bit uncertain how REBOL actually perform cloning when cloning to itself - whether it was assigning two chunks of memory for the clone operation or whether it was merely reassigning the names and then appending extra items in the spec to itself. Regards, James.