Syntax

	$object.copy()

$object.copy($context, $filter, $constructor)

Description

Creates a deep copy of the object or a simple copy of the value object.

In a deep copy of an object, all objects reachable via composition references are also copied. Other references in the copied object graph are transposed to objects in the new graph if they pointed in the original graph to an object that was copied along with the copy operation.

Parameters

Name Type Description Mandatory Default
object Number/string/boolean/business object/set The object or value to be copied. yes
context Business object The context object that will be passed when creating the top-level copy (from object). no null
filter Function

Decides whether a particular property should be copied along from the source graph. The filter function receives three arguments.

  • The attribute to copy.
  • The instance to copy from the source graph.
  • The context instance where the attribute to be copied is defined.

If the decision is true, the attribute is copied from the source object to the target object. If the attribute is a primitive property, the value is copied directly to the target object. If the attribute is a reference, that reference is either copied directly, or rewritten into the target graph, depending on whether the source object was copied in the copy operation or not. If the attribute is a composition, a deep copy of the origin object is made.

If the decision is false, the attribute remains uninitialized in the target object. In this case, the constructor function could calculate a default value for this attribute and set it together with the construction.

no attribute -> orig -> context -> true
constructor Function

A function that takes an object from the source graph and returns an (uninitialized) copy of that object. The function receives three arguments:

  • The original object to be copied.
  • The context reference by which the original object is linked to its context (null, for the top-level copy).
  • The original context object that holds the original object to be copied in its context reference.

If the constructor function returns null, an object of the same type as the original is created.

no orig -> reference -> context -> null

Return value

Type: Number/string/boolean/business object/set

Depending on the type of x, the following is returned by the function:

Examples

Object copy

	{
   a = new(`my.module:MyClass`);
   $a.set(`my.module:MyClass#name`, "orig");
   b = $a.copy();
   $b.set(`my.module:MyClass#name`, "copy");
   list($a, $b);
}

Output: Outputs a list with the original and the copied object: [orig, copy]

Object list copy

	{
   a = new(`my.module:MyClass`);
   b = new(`my.module:MyClass`);
   list = list($a, $b);
   $list.copy();
}

Output: A list with copies of the objects a and b.

Value copy

	{
   a = 5;
   b = $a.copy();
   list($a, $b);
}

Output: A list with the values [5, 5] - one time the original number and one time the copied one.