by codeflo · 4y ago · 0 comments
In the same spirit, I find the article you linked to a bit silly, at least the examples they picked. Following the logic above, there shouldn't even be a "HeadError" exposed anywhere up the call chain. Inventing a complicated mechanism to propagate the error upwards is the opposite of what you want to do; you want elegant ways to handle the problem locally. Having a special singleton HeadError isn't wrong, but I think "Maybe a" would also be a perfectly fine return value for head (as I mentioned in a sibling post, that's what Rust does): head can only "fail" if the list is empty, so there is no actual information in the "error" value.
One error handling strategy not often employed is to prefer code that is correct by construction. It can’t always be done but it’s nice when you can do it.
update spelling
At least as a beginner, the information about which line the error occurred on would be helpful.