Mojo trait
ExplicitlyCopyable
The ExplicitlyCopyable trait denotes a type whose value can be copied explicitly.
Unlike Copyable
, which denotes types that are implicitly copyable, an
explicitly copyable type can only be copied when the explicit copy
initializer is called intentionally by the programmer.
An explicit copy initializer is just a normal __init__
method that takes
a read-only
argument of Self
.
Example implementing the ExplicitlyCopyable
trait on Foo
which requires
the fn(self) -> Self
method:
struct Foo(ExplicitlyCopyable):
var s: String
fn __init__(out self, s: String):
self.s = s
fn copy(self) -> Self:
print("explicitly copying value")
return Foo(self.s)
You can now copy objects inside a generic function:
fn copy_return[T: ExplicitlyCopyable](foo: T) -> T:
var copy = foo.copy()
return copy
var foo = Foo("test")
var res = copy_return(foo)
explicitly copying value
Implemented traits
AnyType
,
UnknownDestructibility
Aliases
__del__is_trivial
alias __del__is_trivial
A flag (often compiler generated) to indicate whether the implementation of __del__
is trivial.
The implementation of __del__
is considered to be trivial if:
- The struct has a compiler-generated trivial destructor and all its fields
have a trivial
__del__
method.
In practice, it means that the __del__
can be considered as no-op.
Methods
copy
copy(self: _Self) -> _Self
Explicitly construct a copy of self.
Returns:
_Self
: A copy of this value.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!