Documentation for routine clone

Documentation for routine clone, assembled from the following types:

class Mu

From Mu

(Mu) method clone

method clone(*%twiddles)

Creates a shallow clone of the invocant. Alternative values for public attributes can be provided via named arguments with names matching the attributes' names.

class Point2D {
    has ($.x$.y);
    multi method gist(Point2D:D:{
my $p = => 2=> 3);
say $p;                     # OUTPUT: «Point(2, 3)␤» 
say $p.clone(=> -5);      # OUTPUT: «Point(2, -5)␤» 

class Array

From Array

(Array) method clone

Defined as:

method clone(Array:D: --> Array:D)

Clones the original Array. Modifications of elements in the clone are not propagated to the original and vice-versa:

my @a = <a b c>my @b = @a.clone;
@b[1= 42@a.push: 72;
say @b# OUTPUT: «[a 42 c]␤» 
say @a# OUTPUT: «[a b c 72]␤» 

However, note that the reifier is shared between the two Arrays, so both Arrays will have the same elements even when each is randomly-generated on reification and each element will be reified just once, regardless of whether the reification was done by the clone or the original Array. Note: just as reifying an Array from multiple threads is not safe, so is, for example, reifying the clone from one thread while reifying the original from another thread is not safe.

my @a = 1{rand} … ∞; my @b = @a.clone;
say @b[^3]; # OUTPUT: «(1 0.0216426755282736 0.567660896142156)␤» 
say @a[^3]; # OUTPUT: «(1 0.0216426755282736 0.567660896142156)␤» 

class Date

From Date

(Date) method clone

Defined as:

method clone(:$year:$month:$day:&formatter)

Creates a new Date object based on the invocant, but with the given arguments overriding the values from the invocant.

say'2015-11-24').clone(month => 12);    # OUTPUT: «2015-12-24␤» 

class DateTime

From DateTime

(DateTime) method clone

Defined as:

method clone(:$year:$month:$day:$hour:$minute:$second:$timezone:&formatter)

Creates a new DateTime object based on the invocant, but with the given arguments overriding the values from the invocant.

say'2015-12-24T12:23:00Z').clone(hour => 0);
# OUTPUT: «2015-12-24T00:23:00Z␤» 

Note that this can lead to invalid dates in some circumstances:

say"2012-02-29T12:34:56Z").clone(year => 2015);
CATCH { default { put .^name''.Str } };
# OUTPUT: «X::OutOfRange: Day out of range. Is: 29, should be in 1..28␤»