ŚCLONE Create copies of object


Implemented for Internal and External classes. Not implemented for System classes.

Syntax:

    objrefs „ objref.ŚCLONE N
    objrefs „ ŚCLONE N     (Within user-defined method, same as ŚTHIS.ŚCLONE N)

The monadic system method ŚCLONE creates new copies of an object, and returns a vector of references to the new objects. The right argument N is the number of copies required, as a positive integer (or 0). The new objects have their properties initially set to the same values as the original.

In this example, we create a simple class Point, and create an instance of it. The instance is then cloned four times. Note that the four copies are independent objects; changing a property of one of them does not affect the others:

      'Point' ŚIC ŚBOX 'X Y Z'
1 1 1
      PT„ŚNEW 'Point'
      PT.X„34 Ş PT.Y„23 Ş PT.Z„12
      VEC„PT.ŚCLONE 4
      VEC
[Point] [Point] [Point] [Point]
      VEC.X
34 34 34 34
      VEC[1].X„11
      VEC.X
11 34 34 34

Contrast this with the following example, in which VEC2 contains four references to the same object:

      VEC2„4˝PT
      VEC2.X
34 34 34 34
      VEC2[1].X„11
      VEC2.X
11 11 11 11

When cloning internal objects, if the properties of the original object contain further object references these sub-objects are not cloned; instead, both the original and cloned objects will point to the same original sub-objects. This is known as a 'shallow' copy operation.

If an object contains file handles or database references, cloning it in this way may not make sense. On the other hand, for simple objects it can be a very useful way of rapidly creating new instances with an initial set of known properties.

ŚCLONE can be used with External classes (but not System classes). However, not all classes implement the operation, and the exact way in which it works may vary according to the architecture and the class:

.Net

In the .Net architecture, ŚCLONE is mapped to the Clone method of the ICloneable interface. However, many .Net classes do not implement this.

Java

For Java classes, ŚCLONE is mapped to the Clone method of the Java base object, which may be over-ridden for a particular Java class.

In this example, we create an instance of the Java Date class (which defaults to the current date/time), clone 12 copies of it, and set the month of each copy to a different value:

      date„'java' ŚNEW 'java.util.Date'
      date
[java:Date]
      dateList„date.ŚCLONE 12
      dateList.setMonth ((Ľ12)-ŚIO)
      12 1˝dateList.ŚDS
 Wed Jan 10 14:33:12 GMT 2007
 Sat Feb 10 14:33:12 GMT 2007
 Sat Mar 10 14:33:12 GMT 2007
 Tue Apr 10 14:33:12 BST 2007
 Thu May 10 14:33:12 BST 2007
 Sun Jun 10 14:33:12 BST 2007
 Tue Jul 10 14:33:12 BST 2007
 Fri Aug 10 14:33:12 BST 2007
 Mon Sep 10 14:33:12 BST 2007
 Wed Oct 10 14:33:12 BST 2007
 Sat Nov 10 14:33:12 GMT 2007
 Mon Dec 10 14:33:12 GMT 2007

R

ŚCLONE is mapped to the duplicate function of R. It can thus be used to create multiple independent copies of R arrays, complex numbers, data frames, factors and so on.

      c„'r' Śnew 'complex' 2 3
      c
[r:complex]
      c.Śds
2+3i
      t„c.Śclone 5
      t.Śds
 2+3i 2+3i 2+3i 2+3i 2+3i
      t
[r:complex] [r:complex] [r:complex] [r:complex] [r:complex]

Ruby

ŚCLONE is mapped to the dup method of the Ruby base object. Again, this may be over-ridden for a particular Ruby class.

In this example, we create a Ruby array (referenced by A), and place three strings in it. We then make a clone of it (referenced by B), which creates an independent copy of the array. Therefore, when we add a fourth element to the array referenced by A, the copy is unchanged:

      A„'ruby' ŚNEW 'Array'
      dummy„A.push 'First'
      dummy„A.push 'Second'
      dummy„A.push 'Third'
      A.length
3
      A.ŚVAL
 First Second Third
      B„A.ŚCLONE 1
      B.length
3
      B.ŚVAL
 First Second Third
      dummy„A.push 'Fourth'
      A.length
4
      A.ŚVAL
 First Second Third Fourth
      B.length
3
      B.ŚVAL
 First Second Third

Topic: APLX Help : Help on APL language : System Methods : ŚCLONE Create copies of object
[ Previous | Next | Contents | Index ]