Lyricdev
GitHub

LYR-SEM0131

a type test that asks nothing an interface value could answer

error · spec 05-interfaces

An 'is' test or a type pattern is applied to a value that is no interface value (its type is known already), or tests against a type parameter or a type that never stands behind an interface value.

Explanation

x is Circle and the pattern c: Circle ask an interface value which concrete type it holds. On a value whose static type is a struct, an enum, an optional or another non-interface type, the answer is known at compile time; on an optional, test for null first. The tested type is one a value behind an interface can have — a struct, a class, an enum, a built-in type, or an interface usable as a value type — and never a type parameter, which has no run-time identity of its own.

Remove the test, test an interface value, or, inside a generic function, constrain the type parameter instead of testing it.

Wrong

struct Circle { r: int }

fn main(): void {
    let c = Circle { r = 1 };
    let _ = c is Circle;
}
interface Shape { fn area(): int; }

struct Circle :: [Shape] {
    r: int,
    fn area(): int { return 3 * this.r * this.r; }
}

fn main(): void {
    let s: Shape = Circle { r = 1 };
    let _ = s is Circle;
}