IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

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

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

You can now construct a generic Defaultable type:

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

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

Implemented traits

AnyType

Required methods

__init__

__init__(out self: _Self)

Create a default instance of the value.

Returns:

_Self