In DateTime§
See primary documentation in context for method clone
method clone(DateTime: :, :, :, :, :, :, :, :)
Creates a new DateTime
object based on the invocant, but with the given arguments overriding the values from the invocant.
say DateTime.new('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 DateTime.new("2012-02-29T12:34:56Z").clone(year => 2015);CATCH ;# OUTPUT: «X::OutOfRange: Day out of range. Is: 29, should be in 1..28»
In Match§
See primary documentation in context for method clone
method clone()
Clones the Match
object.
In Date§
See primary documentation in context for method clone
method clone(Date: :, :, :, :)
Creates a new Date
object based on the invocant, but with the given arguments overriding the values from the invocant.
say Date.new('2015-11-24').clone(month => 12); # OUTPUT: «2015-12-24»
In Mu§
See primary documentation in context for method clone
multi method clone(Mu: *)multi method clone(Mu: *)
This method will clone type objects, or die if it's invoked with any argument.
say Num.clone( :yes )# OUTPUT: «(exit code 1) Cannot set attribute values when cloning a type object in block <unit>»
If invoked with value objects, it creates a shallow clone of the invocant, including shallow cloning of private attributes. Alternative values for public attributes can be provided via named arguments with names matching the attributes' names.
my = Point2D.new(x => 2, y => 3);say ; # OUTPUT: «Point(2, 3)»say .clone(y => -5); # OUTPUT: «Point(2, -5)»
Note that .clone
does not go the extra mile to shallow-copy @.
and %.
sigiled attributes and, if modified, the modifications will still be available in the original object:
my = Foo.new;with my = .clone# Hash and Array attribute modifications in clone appear in original as well:say ;# OUTPUT: «Foo.new(foo => 42, bar => ["Z", "Y"], baz => {:X("W"), :Z("Y")}, …»say ;# OUTPUT: «Foo.new(foo => 70, bar => ["Z", "Y"], baz => {:X("W"), :Z("Y")}, …».boo.(); # OUTPUT: «Hi».boo.(); # OUTPUT: «Bye»
To clone those, you could implement your own .clone
that clones the appropriate attributes and passes the new values to Mu.clone
, for example, via nextwith
.
my = Bar.new( :42quux );with my = .clone# Hash and Array attribute modifications in clone do not affect original:say ;# OUTPUT: «Bar.new(quux => 42, foo => ["a", "b"], bar => {:a("b"), :c("d")})»say ;# OUTPUT: «Bar.new(quux => 42, foo => ["Z", "Y"], bar => {:X("W"), :Z("Y")})»
The |%_
is needed to slurp the rest of the attributes that would have been copied via shallow copy.
In Array§
See primary documentation in context for method clone
method clone(Array: --> Array)
Clones the original Array
. Modifications of elements in the clone are not propagated to the original and vice-versa:
my = <a b c>; my = .clone;[1] = 42; .push: 72;say ; # OUTPUT: «[a 42 c]»say ; # 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 = 1, … ∞; my = .clone;say [^3]; # OUTPUT: «(1 0.0216426755282736 0.567660896142156)»say [^3]; # OUTPUT: «(1 0.0216426755282736 0.567660896142156)»