3 ms·
foo().unwrap_or_else(|err| { // handle err }); or foo() catch |err| { // handle err }
by deagle50 3y ago
foo().unwrap_or_else(|err| {
// handle err
});
or
foo() catch |err| {
// handle err
}
- tail_exchange 3y agoI like the second example, but without the |, so no new syntax is necessary. I don't like the first first one. It's too verbose, and it looks like it's trying to introduce a shorthand for lambdas.
- deagle50 3y agoWas just showing how Rust and Zig do it. Go capture could omit the ||.
- tail_exchange 3y agoI've seen proposals that were very similar. I think the problem with this approach is that it doesn't really make error handling that much easier. Typing "catch err" is not that different from typing "if err != nil".
- Cthulhu_ 3y agoIn Go, if a function returns only an error, you can do if err := foo(); err != nil { // handle error } However, this only works in that particular case, if foo() also returns a value you can no longer use this because the value (and err) are scoped to the if block.