• sunbeam60@lemmy.one
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    8 months ago

    I do think Zig is better for this kind of thing.

    const ret = try do_thing();
    
    if( ret ) | result | {
       do_something_with_result(result);
    }
    

    The try keyword returns any error up; the if-unwrap works with what came out of a successful call. Normally you wouldn’t have both, of course.

    do_thing would be defined as a union of an error (a distinct kind of type, so it can be reasoned about with try, catch and unwrapping) and the wrapped return value.