3 ms·
I agree. `crop_to_cat(img)` now has to be concerned with handling a possibly invalid image even though this method doesn't make sense for an invalid image. It m
by dthul 5y ago
I agree. `crop_to_cat(img)` now has to be concerned with handling a possibly invalid image even though this method doesn't make sense for an invalid image.
It muddies the type signature of the method and instead of `Image -> Image` (or possibly `Image -> Either Image Error`) it has to read `Either Image Error -> Either Image Error`.
What if I want to apply that method to a valid image?
I either need to wrap the image or provide different overloads of the method which can get quite tedious if the number of possibly "fallible" parameters grows.
A macro for unwrapping and early return would be nicer.
- amadvance 5y agoNot to mention that you also lose the error context. That boolean "is_valid" flag is not really informative of what happened. For example, if the image to load doesn't exist, you may want to print/log such path. The goto looks like a better approach.
- flohofwoe 5y agoYou can just as well use a nested common error struct which has more detailed error information instead of just an "is_valid" bool, and also turn the whole return value into a tagged union to save some space (so the return value either contains a success value, or error data, but not both). You just don't have the high-level syntax sugar to create or deal with tagged unions in C.