Lasso Soft Inc. > Home

[ any->asCopy ]

Method

The [any->asCopy] method returns a shallow copy of an object. In the rare circumstance that a deep copy is required, use the [any->asCopyDeep] method.

In Lasso, a type has depth if it contains other objects, so the [integer] and [string] types, for example, have no depth. This means that [any->asCopy] is the same as [any->asCopyDeep] for these types. Other types, such as the [array] type, do contain other objects and therefore have depth.

The [any->asCopy] performs a shallow copy which means that the returned copy refers to the same objects as the original object. The original object can be modified without affecting the copied object. For example, remove an element from the original array and it will still exist in the copied array. However, modify an object inside the original object, that object will also be changed in the copied object.

For example:

var(original) = array('moose', 2, map('hair'='wool'), 'jazz')
   var(copy) = $original->asCopy
   $original->removeAll(2)
   $original->get(2)->find('hair')->uppercase
   $original->get(3)->uppercase
   $copy

Produces:

   array(moose, 2, map(hair = WOOL), JAZZ)

  • Syntax
local(obj) = my_object(...)
#obj->asCopy
Examples
  • Beginner

Demonstrating a shallow copy

The code below demonstrates what happens with a shallow copy. The result will show that the copied array's elements are the same as the original's.

Code

local(original) = array('radish', 'carrot', 'cucumber', 'olive')
local(copy)     = #original->asCopy
iterate(#original) => {
    loop_value->uppercase
}
#original
'<br>'
#copy

Result

array(RADISH, CARROT, CUCUMBER, OLIVE)
array(RADISH, CARROT, CUCUMBER, OLIVE)

Recent Comments

No Comments found

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft