Object copy

Syntax

$orig.copy()

copy(orig: $orig,
	context: $context, 
	filter: $filter, 
	constructor: $constructor, 
	transient: false)

Description

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

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
orig 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 orig). 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 null, an object of the same type as the original is created.

no orig -> reference -> context -> null

transient

Boolean Whether the copy should create a transient object. This option is only relevant if no const function is specified. If nothing is specified (null), transient objects are copied as transient and persistent ones as persistent. Transient objects are visible only locally in the user's session and cannot be stored in the reference of a persistent object. Conversely, however, a transient object can reference normal persistent objects in its references. no null

Return Value

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

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

  • Reference to a business object: a deep copy of this object.
  • list: a list in which all business objects are replaced by deep copies.
  • all other values: the value itself.

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 of copies of the objects a and b.

Value copy

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

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