Lyricdev
GitHub

LYR-SEM0029

a pattern that cannot match the scrutinee's type

error · spec 09-patterns

A pattern's shape does not fit the scrutinee's type: null against a non-optional, a literal or range of another type, a tuple pattern of another arity, an array pattern against a non-array, or a dotted variant path against a type that is not an enum.

Explanation

A pattern is checked against the scrutinee's type before it is tried; a pattern that could never match a value of that type is a mistake in the pattern or in the scrutinee.

Write the pattern for the scrutinee's type — null only against ?T, literals of the scrutinee's type, a tuple pattern with as many elements as the tuple.

Wrong

enum Signal { Red, Green }

fn code(s: Signal): int {
    return match (s) { null => 0, _ => 1 };
}

fn main(): void {
    let _ = code(Signal.Red);
}
enum Signal { Red, Green }

fn code(s: ?Signal): int {
    return match (s) { null => 0, _ => 1 };
}

fn main(): void {
    let _ = code(Signal.Red);
}