The move()
method in D
language copies a
source
value into the target
. It can also do this via a type of copy known as a destructive copy when necessary. This kind of copy occurs when source
is defined with a destructor. The source
is reset to its .init
value after it is moved into target
. Otherwise, it is left unchanged.
Note: An assertion failure will be thrown if the source value has internal pointers that point to itself and doesn’t define an
opPostMove
method, which will give it the permission to do so. This will cause thesource
not to be moved and trigger the earlier mentioned error.
move(source,target)
source
: This is a struct object value whose elements will be copied into another value.target
: This is the variable into which the values of the source
will be copied.The move()
method returns a struct object value with the value of the source
object struct copied into it.
void main(){import std.algorithm.mutation;import std.stdio: write, writeln, writef, writefln;// Structs without destructors are simply copiedstruct Struct_Value{int c = 1;int d = 2;}Struct_Value val1 = { 10, 11 };Struct_Value val2;move(val1, val2);writeln(val2);writeln(val1);}
Struct_Value
and two struct
objects val1
and val2
.move()
function to copy all the values of val1
into val2
.val1
and target val2
after move()
method operation.