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
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)defaultImplemented traitsโ
AnyType,
ImplicitlyDestructible
Required methodsโ
__init__โ
__init__(out self: _Self)
Create a default instance of the value.
Returns:
_Self
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!