Lyricdev
GitHub

LYR-PAR0050

a 'try' that changes the value, not at the start

error · spec 06-errors

try? or try!, or a try with catch clauses, stands to the right of an operator instead of at the start of the expression it covers.

Explanation

A plain try marks what may throw and changes no value, so it may stand to the right of an operator. try? turns the value into an optional, try! panics on an error, and try … catch gives the clauses' value: what they cover has to be beyond doubt, so they stand at its start — try? a + b, not a + try? b.

Move the try to the start of the expression it covers, or put that expression in parentheses.

Wrong

enum Oops :: [Error] { Bad; fn message(): string { return "oops"; } }

fn one(): int throws Oops {
    return 1;
}

fn main(): void {
    let n = 1 + try? one();
}
enum Oops :: [Error] { Bad; fn message(): string { return "oops"; } }

fn one(): int throws Oops {
    return 1;
}

fn main(): void {
    let n = try? 1 + one();
    let _ = n;
}