Comments on: Object APPEND, COPY, LENGTH?

Carl Sassenrath, CTO
REBOL Technologies
23-Mar-2007 21:17 GMT

Article #0073
Main page || Index || Prior Article [0072] || Next Article [0074] || 13 Comments || Send feedback

In addition to standard object actions, R3 allows:

 appendextends the object with new word/value pairs (the input object is modified). New words must not already be fields of the object, or an error will result. Words can be word or set-word values. No binding is done on the data value parts (so words within blocks and functions do not get bound to the new context).
 copyclone the object. Deep copy all series within it. Currently, no rebinding is done. (Words within blocks and functions do not get bound to the new context.)
 length?return the number of instance variables. (Faster and less garbage than doing a length? first operation.)

Example of append:

>> a: make object! [b: 10]
>> append a [c: 20]
== make object! [
        b: 10
        c: 20

To get the length:

>> length? a
== 2

The copy function works as you would expect.

Why not insert, remove, etc.?

Why not allow insert and other series modifying functions to work on objects? Because they would corrupt the bindings of words within the object.



Brian Hawley
23-Mar-2007 19:36:25
I assume you can bind stuff to the new fields after you do the append - this would handle the binding problem. It seems that the copy action acts like bind/copy but without the bind. This all seems good to me.
Jeff M
23-Mar-2007 22:25:44
Perhaps this is just me, but this very much feels like a solution without a problem. What use would this have? When would I want to do this? Also, the fact that other operations don't work the same way because of corruption problems signals a problem to me.

If others that use REBOL far more than I do find this extremely useful, I say "great," because I'm sure eventually I'll run into the use. Otherwise, again, I see this is a just a feature looking for a problem to solve.

24-Mar-2007 4:27:21
I was looking for this functionality a long time ago, when attempting to build an object based database, but the design was too complex, so I dumped it. However, I recently built a new one, that could take good advantage of this feature.

Jeff, if it works the same way as:

>> a: make object! [b: 10]
>> a: make a [c: 20]

You haven't saved any code, but by using APPEND bindings are kept and modifying an object with APPEND is potentially faster than re-making it. If I'm not mistaken, it also saves a bit of memory. If there is no speed, memory or binding benefit to it, then I think there is little reason to introduce this feature.

OTOH, I remember last time this was brought up, that it would introduce a security hole. Was it Brian Hawley who mentioned it? I don't recall.

Carl, what would happen here:

>> a: make object! [b: 20]
>> append a [b: 25]

The end result should overwrite 'b, but we're appending. This is not consistent with traditional block behavior.

24-Mar-2007 7:53:06
Jeff: This is actually very useful and it's something that was not possible in R2 because of the implementation. Now that it's possible it's great to have ways to do it. APPEND is not something that you use very often maybe (but I would have used it a lot of times), while COPY is just nice to have (instead of using MAKE). LENGHT? is probably less useful in practice... but why not?
Brian Hawley
24-Mar-2007 15:08:14
As I recall Henrik, it was more of a stability and predictability concern, though security would be a factor too. Fortunately the APPEND behavior listed above would not be a problem, because "No binding is done on the data value parts". Rebinding the existing words in the object would change the behavior of those words, the equivalent of code injection. This would make sandboxes nearly impossible and magnify the effects of unintentional bugs. If there is no automatic rebinding of functions, no problem.

If REBOL added some kind of visibility rules I'd have to see how APPEND would interact with those before I could speculate about the security implications. It seems unlikely to be an issue though: The proposals so far do visibility restrictions by module, not on an object level.

Anton Rolls
25-Mar-2007 1:26:51
Yep, this is very useful.
25-Mar-2007 6:34:19
I need this kind of object manipulation functions.

I wrote an 'extends function :

c: extends a b

'c is an object composed by elements of object 'a and by elements of object 'b that are not already in 'a. It imitate a bit other OO language behaviour. Usually I use this syntax:

a: extends a b

I use also a lot the 'clone script found on that will be deprecated with the new 'copy function. It does exactly the same thing but with 'copy R3 is more consistent.

'append and 'copy are definitively useful. I'm not convinced by 'length?

Other series functions that manipulate series seem not interesting with objects. For an object, what's the difference between 'insert and 'append when the order of objects elements is not important?

25-Mar-2007 8:23:26
I would suggest protecting /self from being modified if we are going this route or at least throwing an error. Currently anyone that creates an object can go back to that object and set object/self: to any value.
26-Mar-2007 17:58:12
My guess is, that the majority of people requesting this actually need an associative array?
Gregg Irwin
26-Mar-2007 18:16:15
Associative arrays are really useful, I agree. That's come up before as well, and is something I think is more important to the usability of REBOL than a lot of other things; and is feature people expect to be there when they come from other languages. Obviously we don't *need* it, but it's darn handy and can look like a big oversight as a core datatype.
Brian Hawley
26-Mar-2007 19:09:14
Ladislav, I have to admit that using this for an associative array never occurred to me.

I was thinking more of occasions where you would need to extend objects created by another framework, such as VID. You may need to add some fields to some existing objects that would be used by your extended behavior, but you may not be able to replace the existing objects because they may be referred to all over the place. Being able to extend an object in place would be perfect then.

Anyways, I thought that they were going to add an associative array type - wasn't it proposed to possibly replace the hash! type, or be a new dict! type?

27-Mar-2007 22:16:11
No, i need an object. Or i consider an object an associative array. But i need it smarter, noerror for existing words. I want it to work like make:

old-object: make old-object [this: 1 that: 2]

but i dont like the relocation, which breaks all references. I want it in place. Main use is to sync data on client and server. when the server creates an object and extends it, i want to send it to the client and simply apply the changes. Currently i use 'make, but that is expensive and needs a lot tracking of existing references.

3-Apr-2007 2:42:22
yes, it looks, that the object extensibility may be widely used in the future, apologizing for my inappropriate question

Post a Comment:

You can post a comment here. Keep it on-topic.


Blog id:



 Note: HTML tags allowed for: b i u li ol ul font span div a p br pre tt blockquote

This is a technical blog related to the above topic. We reserve the right to remove comments that are off-topic, irrelevant links, advertisements, spams, personal attacks, politics, religion, etc.

Updated 15-Jun-2024 - Edit - Copyright REBOL Technologies -