4 ms·
I was actually wondering (in Zig, which has a per-statement "try" which is essentially the same as the ? in Rust) whether it also makes sense for whole blocks,
by flohofwoe 4y ago
I was actually wondering (in Zig, which has a per-statement "try" which is essentially the same as the ? in Rust) whether it also makes sense for whole blocks, which would look a lot like traditional try-catch block in languages with exceptions, e.g. instead of:
try may_fail_1();
try may_fail_2();
try may_fail_3();
...this could be grouped into:
try {
may_fail_1();
may_fail_2();
may_fail_3();
}
...but would behave exactly the same as the indiviual trys, if any function in the block returns with an error, that same error is passed up to the caller. But I guess that forcing individual trys makes you think harder about handling individual errors than just pushing the responsibility for error handling up the callstack.
- gpderetta 4y agoSure, but we have now (almost) gone full circle:). When writing exception safe code, for me is more important to know which functions are guaranteed not to fail as they will be called in the commit path. Currently I just comment which operations are no-throw and otherwise assume that everything else can fail, but it would be nice to have the compiler tell me.
- codethief 4y ago> for me is more important to know which functions are guaranteed not to fail as they will be called in the commit path But Zig allows you to see immediately which functions are guaranteed not to fail because those functions's return types won't include an error type.
- codethief 4y agoI've been pondering the same thing![0] Essentially, one would get (checked-)exception-like behavior, except that performance would be better, and whether a function can fail or not would still need to be declared explicitly in its return type. > But I guess that forcing individual trys makes you think harder about handling individual errors than just pushing the responsibility for error handling up the callstack. It probably does but it can also make code much harder to read if you have to check for errors after every other line. I'm a bit divided on this. [0]: https://news.ycombinator.com/item?id=34856910 https://news.ycombinator.com/item?id=34856910