Skip to main content
Version: Nightly

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, ImplicitlyDestructible

Required methods

__init__

__init__(out self: _Self)

Create a default instance of the value.

Returns:

_Self