Mojo trait
AnyType
The AnyType trait describes a type that has a destructor.
In Mojo, a type that provide a destructor indicates to the language that it is an object with a lifetime whose destructor needs to be called whenever an instance of the object reaches the end of its lifetime. Hence, only non-trivial types may have destructors.
Any composition of types that have origins is also an object with a lifetime, and the resultant type receives a destructor regardless of whether the user explicitly defines one.
Unless they specify @explicit_destroy, all Mojo structs and traits are considered to inherit from AnyType, providing a default no-op destructor implementation for types that may need them.
Example implementing the AnyType
trait on Foo
that frees the
allocated memory:
@value
struct Foo(AnyType):
var p: UnsafePointer[Int]
var size: Int
@implicit
fn __init__(out self, size: Int):
self.p = UnsafePointer[Int].alloc(size)
self.size = size
fn __del__(owned self):
print("--freeing allocated memory--")
self.p.free()
@value
struct Foo(AnyType):
var p: UnsafePointer[Int]
var size: Int
@implicit
fn __init__(out self, size: Int):
self.p = UnsafePointer[Int].alloc(size)
self.size = size
fn __del__(owned self):
print("--freeing allocated memory--")
self.p.free()
Implemented traits
UnknownDestructibility
Methods
__del__
__del__(owned self: _Self, /)
Destroy the contained value.
The destructor receives an owned value and is expected to perform any actions needed to end the lifetime of the object. In the simplest case, this is nothing, and the language treats the object as being dead at the end of this function.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!