Advanced Computing Environment
Hosted by SourceForge
brix-os project page

Previous: Casting ----- Up: Contents ----- Next: Projectors


show notes
Features that need to be added to the page or ideas that haven't been thought out.
  • types have deep and shallow replicator properties. the shallow replicator does a simple memcpy to create an exact clone of the object, including references. the deep replicator does the same memcpy and then invokes the deep replicators for each reference slot and updates the references with the new clones. the default deep replicator is disabled (and throws an exception) for types that use untyped slots, such as Array and Number. these types must implement their own deep replicator that understands the type's structure and knows what is a reference. the shallow replicator may also be re-implemented to prevent cloning, add clone tracking, modify slots or some other rare ability.

Objects can be copied as a reference, destructured into specific components when requested by a constructor or explicitly copied with the COPYOF extension. Copying an object will always make a duplicate of the object's structure including the pointers in reference slots. This produces a shallow copy of the object and it shares the referenced objects with the original. A shallow copy can be produced faster but isn't always the desired result, as changes to one will effect the other. Therefore, the default cloning mode is always a deep copy and shallow copies must be explicitly requested. A clone is always returned even if a reference is passed to COPYOF.

`shallow COPYOF object
  • `shallow -- optional flag to make shallow copies
  • object -- original object to clone
	a := [1,2,3]
	b := copyOf a // deep clone
	c := `shallow copyOf a // shallow clone

The above code produces three unique objects since each element contains a cloned value. In the code below, all three objects are unique but a and c share the same references to "foo" and "bar". Modifying the referenced objects in one will change them in the other.

	a := ["foo", "bar"]
	b := copyOf a // deep clone
	c := `shallow copyOf a // shallow clone

	a[1][2] = 'z'
	echo b[1] c[1]; // displays "barbaz"

...see notes...

Previous: Casting ----- Up: Contents ----- Next: Projectors