REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 27-Aug-2009 Edit History  

REBOL 3 Datatypes: Object!

This datatype has not yet been documented for R3.

Contents

Concept

context of names with values

Format

Creation

There are a couple issues to be addressed with object creation:

  1. Should objects deep copy values found in their body (init) blocks?
  2. Should objects deep copy values found in their parent object?

The best way to analyze this situation is to create a test case. The test below determines how the major aggregate types are handled:

b1: reduce/no-set [
    str: "abc"
    bin: #{010203}
    blk: [a b c]
    vec: make vector! [integer! 32 [1 2 3 4]]
    img: make image! [2x1 #{010101 020202}]
    obj: make object! [data: "abc"]
]

o1: make object! b1
o2: make object! b1
o3: make o1 []
o4: make o1 b1

compare: func ['a 'b][
    print ["comparing" a "to" b]
    a: get a
    b: get b
    foreach [word val] a [
        if same? :val get in b word [
            print ["  " word "field is shared"]
        ]
    ]
    print ""
]

compare o1 o2
compare o1 o3
compare o1 o4

In A79, the results are:

comparing o1 to o2
   str field is shared
   bin field is shared
   blk field is shared
   vec field is shared
   img field is shared
   obj field is shared

comparing o1 to o3
   vec field is shared
   img field is shared

comparing o1 to o4
   str field is shared
   bin field is shared
   blk field is shared
   vec field is shared
   img field is shared
   obj field is shared

So, the values of the object body block are not copied, but the values of parent fields are copied, with the exception of vectors and images.

Comparing with R2 the difference (other than vector! missing in R2) is in the second case:

comparing o1 to o3
   obj field is shared

So, even the image is copied (although there is a bug in R2 in this regard.) It should also be noted that block values are deep copied in both R2 and R3.

If our goal for R3 is to remain "almost compatible" with R2, then we need to copy images and not copy objects. Also, to be consistent so that users don't have to remember extra rules, we should also copy vectors.

We can now ask these questions:

  1. What if the user does not want any copying?
  2. What if the user wants to copy everything?

Clearly, we need a few other options. How best to provide them?

If we don't want to add refinements to make, then perhaps we can use copy to provide some of the functionality?

If we copy of an object and do not specify /deep then the object is copied but none of its values are copied. Everything is shared.

If we use /deep, then all of the source object's values are deep copied. (But, it should be noted that nothing is rebound to the new object. You would need to do that step manually.)

In summary:


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin