Skip to main content

Move Semantics

The move keyword transfers ownership of a value from one variable to another. After the move, the original variable is invalidated.


Syntax

x is 100;
y is move x; // ownership transferred to y

Print(y); // 100
Print(x); // undefined — x has been moved

How Move Differs from Copy and Alias

StatementBehaviorOriginal after?
y is x;Alias — shared memoryStill valid (shared)
y is another x;Deep copyStill valid (independent)
y is move x;Transfer ownershipInvalidated

When to Use move

  • When transferring ownership of a large data structure without copying
  • When the original variable is no longer needed
  • For resource management patterns (file handles, GPU buffers)

Example

Init method() {
x is 100;
y is move x;
z is x; // z binds to x's (now-moved) memory

Print(y); // 100
Print(z); // may be undefined
}

Shorthand (Without is)

a 100;
b move a; // move without 'is'

C++ Equivalent

MorphC++
y is move x;T y = std::move(x);

Next Steps