Generic structs and enums
Structs and enums may declare type parameters after their names. Each concrete argument produces a checked instantiation that keeps the nominal identity of the declaration.
Expected arguments also guide contextual variants and empty collection payloads, so Box<int> keeps int even when a variant carries no value.
enum Box<T> {
Value(T),
Empty,
}
fn read(box: Box<int>) -> Optional<int> {
match box { .Value(value) => some(value), .Empty => none }
}
fn main() { println(read(.Value(7))) }
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: some(7)
Ctrl/⌘+Enter runs the current lesson.