Capy-Bearer Documentation

clone

lib call

Capy

function clone(value : string) string
function clone(value : any) any
function clone(value : [T]) [T]
function clone(value : struct) struct

Description

clone() makes a deep copy. For an any, an array, or a struct the result and everything inside it are new objects, and changing one side never shows on the other. A closure or module inside the value is shared, since it has nothing to copy.

Assignment never copies these values, it gives the same object another name. Reach for clone() when you want to change a value without changing what you got it from.

Parameters

value : the value to copy

Return Values

A separate copy of the same type.

Example

Capy

struct Point {
    x : s64
    tags : any
}

var source := Point(1, { a: 1 })
var copy := clone(source)
copy.x = 2
copy.tags.a = 2
var list := [1, 2, 3]
var list_copy := clone(list)
list_copy[0] = 9
print(source.x, " ", source.tags.a, " ", copy.x, " ", copy.tags.a, " ", list[0], " ", list_copy[0], "\n")

Output

1 1 2 2 1 9