News for hackers. Stuff that matters.
Re:Guide to Error Handling in Rust by hyujwfasdf on Sunday August 25, 2024 @11:35AM (#41348034)
If Rust had an equivalent to tuples but for sum types I believe the error ergonomics would be better. This would result in more exact error handling instead of a crate wide error enum that has error states that would never happen. Not to mention the chore of constructing enum wrappers for all error cases.

https://youtrack.jetbrains.com/issue/KT-68296/Union-Types-fo...

https://harelang.org/tutorials/introduction#defining-new-err...

Re:Guide to Error Handling in Rust by shepmaster on Sunday August 25, 2024 @12:08PM (#41348383)
I partially agree. I have been experimenting with creating a lot of small error types (e.g. one per function) because of the benefit of narrowing down the set of possible causes. I also dislike crate-wide errors for the same reason.

However, having an anonymous sum type isn’t a good solution here because there’s nowhere for you to add context to. It’s not useful for your final error to say “permission denied” without the context of a file name, or knowing that you were trying to open configuration file, etc.

Re:Guide to Error Handling in Rust by UnquietTinkerer on Sunday August 25, 2024 @12:35PM (#41348654)
Oh look, my old friend Checked Exceptions! I knew we would meet again one day :)

In all seriousness, anonymous sum types would be quite nice. It would make it easier to be precise about return values in situations where `Optional` and `Result` are not sufficient.

The problem with checked exceptions (well, one of the problems) is that the full set of error conditions a function might encounter is often _too_ detailed. Encoding them all into the type signature of every function can be a lot of work, and quite brittle to minor internal changes. So most code bases will still need a catch-call error type that gets bubbled up to a generic handler somewhere.