4 ms·
why does _literally_ every line start with "try"?
by tills13 3y ago
why does _literally_ every line start with "try"?
- dundarious 3y agoSame meaning as the Rust “?” operator.
- tialaramex 3y agoRust's ? is the Try operator, which is currently contemplating Try::branch() to produce a ControlFlow and then if the ControlFlow is Break it returns whatever is inside the Break. Historically ? was just doing what try! did but while that's equivalent to the current behaviour for Result, there are several more implementations of the Try trait today (and in nightly you can implement it on your own types)
- syrrim 3y agoA middle ground between exceptions in c++ and checking every function call for an error code in c.
- defen 3y agoIt's not literally every line, but `try` means "unwrap the error union result or pass the error back up to the caller". It's roughly equivalent to Rust's `?` operator (although Rust's can also unwrap optionals or return `None`)
- zamnos 3y ago~$ curl https://raw.githubusercontent.com/ziglang/zig/7cf2cbb33ef34c1d211135f56d30fe23b6cacd42/test/standalone/http.zig https://raw.githubusercontent.com/ziglang/zig/7cf2cbb33ef34c... 2>/dev/null | wc -l 541 :~$ curl https://raw.githubusercontent.com/ziglang/zig/7cf2cbb33ef34c1d211135f56d30fe23b6cacd42/test/standalone/http.zig https://raw.githubusercontent.com/ziglang/zig/7cf2cbb33ef34c... 2>/dev/null | grep '^[ ]try' | wc -l 114 541 != 114 Did you mean figuratively*?
- tills13 3y agoNo, I clearly meant "literally" Don't be a pedant.
- paulddraper 3y ago* every substantive line
- regulation_d 3y agoI mean if the OED allows for a metaphorical or hyperbolical meaning, I think you're fighting a literal uphill battle.
- laserbeam 3y agoIt makes it obvious that those calls can fail. Every single write to disk, network or malloc can in theory fail. This just makes it obvious that some code needs to handle those edge cases (or, ykno, you can pass them on to the human user).
- martinhath 3y agoNot just in theory, they can, and do, fail in practice.
- laserbeam 3y agoAgreed. Poor choice of word there.
- formerly_proven 3y agoA lot less verbose than “if err != nil { /* 2-3 lines */ }” after every substantive line.
- C-x_C-f 3y agoMore like `catch |err| return err` so not that big of a saving but yeah it's more convenient
- jrockway 3y agoWhat are you doing with lines 1 and 2? Most people just `return fmt.Errorf("stuff the caller can't know: %w", err)` and call it a day. To me, this beats exceptions because a human can provide context; what iteration of the loop were we on, what specific sub-operation failed, etc. Spending a lot of time typing in error handling code seems OK to me. You are never going to get paged when your software is working. So you'll probably spend more of your time dealing with rare error conditions. Thus, the code spends a lot of lines capturing the circumstances around the error conditions, so you can go change a line of code and redeploy instead of spending 6 months debugging.
- zeroxfe 3y agoI don't think they're arguing that exceptions are better, just that `try` (in Zig) or `?` (in Rust) is nicer than `if err != nil { ... }`. It's the one thing about go that I really wish had better ergonomics.
- AndyKelley 3y agowelcome to networking, where every operation can fail
- cturtle 3y agoIn addition to the other answers, this is the most logical way to handle most errors in this specific type of code. The http code in the Zig standard library doesn’t know how the programmer wants to handle all the errors, so it uses try to bubble up any error it doesn’t know how to handle. Why so many uses of try? Because Zig is all about readability, and anytime you see try you _know_ that the called function can possibly fail.
- lenkite 3y agoIn Go, every line would start with "if". In Rust every line would end with "?". I find the anti-exception attitude of newer languages questionable.