Skip to main content

Mojo trait

Defaultable

The Defaultable trait describes a type with a default constructor.

Implementing the Defaultable trait requires the type to define an __init__ method with no arguments:

struct Foo(Defaultable):
    var s: String

    fn __init__(out self):
        self.s = "default"

You can now construct a generic Defaultable type:

fn default_init[T: Defaultable]() -> T:
    return T()

var foo = default_init[Foo]()
print(foo.s)
default

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

__init__

__init__(out self: _Self)

Create a default instance of the value.

Returns:

_Self

Was this page helpful?