Lyricdev
GitHub

LYR-SEM0016

branches whose types do not unify

error ยท spec 08-expressions

The branches of an if expression, the arms of a match expression, the parts of a try expression or the yields and returns of an inferred lambda produce types that have no common type.

Explanation

An expression that chooses between branches has one type, the one every branch fits. Without an expected type, the branches have to agree among themselves: an int in one and a string in the other give the whole expression no type.

Make the branches produce one type, or declare the type of the binding so each branch is checked against it.

Wrong

fn main(): void {
    let flag = true;
    let _ = if (flag) 1 else "one";
}
fn main(): void {
    let flag = true;
    let _ = if (flag) 1 else 2;
}