Movable | Modular
Mojo trait
The Movable trait denotes a type whose value can be moved.
Implement the Movable trait on Foo which requires the
fn __init__(out self, *, deinit take: Self) method:
struct Foo(Movable):
fn __init__(out self):
pass
fn __init__(out self, *, deinit take: Self):
print("moving")You can now use the ^ suffix to transfer owned values instead of copying:
fn return_foo[T: Movable](var foo: T) -> T:
return foo^
var foo = Foo()
var res = return_foo(foo^)Implemented traits
comptime members
__move_ctor_is_trivial
comptime __move_ctor_is_trivial
A flag (often compiler generated) to indicate whether the implementation of move constructor is trivial.
The implementation of a move constructor is considered to be trivial if:
- The struct has a compiler-generated trivial move constructor because all its fields have trivial move constructors.
In practice, it means the value can be moved by moving the bits from one location to another without side effects.
Required methods
__init__
__init__(out self: _Self, *, deinit take: _Self)
Create a new instance of the value by moving the value of another.
Args:
- take (
_Self): The value to move.
Returns:
_Self