Skip to content

Commit

Permalink
add Defaultable trait
Browse files Browse the repository at this point in the history
Signed-off-by: Max Brylski <helehex@gmail.com>
  • Loading branch information
helehex committed May 5, 2024
1 parent fd304d2 commit 0771a45
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions stdlib/src/builtin/value.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,40 @@ trait Copyable:
...


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 (other than self):
```mojo
struct Foo[default: StringLiteral](Defaultable):
var s: String
fn __init__(inout self):
self.s = default
```
You can now construct a generic `Defaultable` type:
```mojo
struct Pair[T1: Defaultable, T2: Defaultable]:
var bleep: T1
var bloop: T2
fn __init__(inout self):
self.bleep = T1()
self.bloop = T2()
var pair = Pair[Foo["Aigis"], Foo["Max"]]()
```
"""

fn __init__(inout self):
"""Create a default instance of the value."""
...


trait CollectionElement(Copyable, Movable):
"""The CollectionElement trait denotes a trait composition
of the `Copyable` and `Movable` traits.
Expand Down

0 comments on commit 0771a45

Please sign in to comment.