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
Describes a type with a default constructor.
The Defaultable trait has a default implementation of __init__() that
uses reflection to default-construct every field. This means simple
structs can conform to Defaultable without implementing any methods:
@fieldwise_init
struct Point(Defaultable):
var x: Int
var y: Int
var p = Point() # x == 0, y == 0
All fields must conform to Defaultable. Override __init__() for custom
default semantics:
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
Provided methods
__init__
def __init__(out self)
Creates a default instance of the value.
The default implementation uses reflection to default-construct all
fields in place. All fields must conform to Defaultable.
Returns:
_Self