Lyricdev
GitHub

LYR-SEM0140

'try?' over a call without a value, used as a value

error · spec 06-errors

'try?' is applied to an expression of type 'void' and its result is used as a value.

Explanation

try? f() turns the value of f() into an optional: null when it threw. A void call has no value to wrap, so there would be nothing but null or nothing to hold. As a statement, try? save(); is allowed: it runs the call and drops the error.

Write the try? as a statement, or use try / a try { … } catch block to learn whether the call failed.

Wrong

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

fn save(): void throws Oops { }

fn main(): void {
    let saved = try? save();
    let _ = saved;
}
enum Oops :: [Error] {
    Bad;
    fn message(): string { return "oops"; }
}

fn save(): void throws Oops { }

fn main(): void {
    try? save();
}