Lyricdev
GitHub

LYR-SEM0031

an enum variant used against its shape

error ยท spec 09-patterns

A variant is constructed or matched in a form its declaration does not have: a struct variant called like a function, a tuple variant built with braces, a payload pattern on a variant without one, or a variant name the enum does not declare.

Explanation

A variant has one of three shapes: no payload (Empty), a tuple payload (Num(int), built and matched with parentheses), or named fields (Rect { w: int, h: int }, built and matched with braces). Construction and patterns use the shape the declaration gives.

Use the declared form, or correct the variant's name.

Wrong

enum Shape { Num(int), Rect { w: int, h: int } }

fn main(): void {
    let s = Shape.Rect(1, 2);
    let _ = s;
}
enum Shape { Num(int), Rect { w: int, h: int } }

fn main(): void {
    let s = Shape.Rect { w = 1, h = 2 };
    let _ = s;
}