Lyricdev
GitHub

LYR-PAR0036

an if expression without 'else'

error · spec 08-expressions

An if in expression position — after =, as an argument, without braces around its branch — has no else branch.

Explanation

An if that gives a value gives one on both paths: if (c) a else b. Without the else there would be no value when the condition is false. An if without else is a statement, and its branches are blocks.

Write the else branch, or make the if a statement.

Wrong

fn main(): void {
    let ok = true;
    let s = if (ok) "yes" "no";
}
fn main(): void {
    let ok = true;
    let s = if (ok) "yes" else "no";
    let _ = s;
}