Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

d language
communitycreator

What is the move() method in D language?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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 the source not to be moved and trigger the earlier mentioned error.

Syntax

move(source,target)

Parameters

  • 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.

Return value

The move() method returns a struct object value with the value of the source object struct copied into it.

Example

void main()
{
import std.algorithm.mutation;
import std.stdio: write, writeln, writef, writefln;
// Structs without destructors are simply copied
struct Struct_Value
{
int c = 1;
int d = 2;
}
Struct_Value val1 = { 10, 11 };
Struct_Value val2;
move(val1, val2);
writeln(val2);
writeln(val1);
}
Example to use move() method in D language

Explanation

  • Line 1 and 19: We start and end the main function.
  • Lines 3 and 4: We import the relevant methods.
  • Lines 6–12: We declare a struct named Struct_Value and two struct objects val1 and val2.
  • Line 14: We use the move() function to copy all the values of val1 into val2.
  • Lines 16 and 17 : We display the values of source val1 and target val2 after move() method operation.

RELATED TAGS

d language
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring