Contextual enum variants
When an expected enum type is known, .Variant is enough for unit or payload construction. A function's return type gives its tail expression that context, just as a type annotation does for a binding. The same shorthand works in patterns, calls, imports, and aliases.
Without expected context, moon accepts the shorthand only when one visible enum owns that variant; otherwise it reports every candidate.
enum Signal { Go, Stop }
fn parse_label(signal: string) -> Signal {
if signal == "go" { .Go } else { .Stop }
}
fn label(signal: Signal) -> string {
match signal { .Go => "go", .Stop => "stop" }
}
fn main() {
let signal: Signal = .Stop
println(label(parse_label("go")))
println(label(signal))
}
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: go stop
Ctrl/⌘+Enter runs the current lesson.