| ▲ | adrianmsmith a day ago | |||||||
People advocate for Go's error handling because it forces you to deal with errors. But in the cases I do want to catch a specific error, the signature only tells me that a function returns an error, not which type. So I do feel that returning (int, error) is strictly worse than Java's checked exceptions if you care about errors. | ||||||||
| ▲ | spockz a day ago | parent | next [-] | |||||||
This is elegantly solved with sum types. (And arguably needs sub typing.) If instead of just returning (int, error) the function would return (int, parseError | outOfBoundsError) you would know that the parser function can fail on reading a number at all and on the number being to big/small to fit the type and handle then accordingly. Saliently, Java in a sense has supported sum types in the throws declaration and the subsequent catch statements forever. Unfortunately it has not landed in other places where you can use types so you cannot use it for returning errors. Scala 3 supports this but has tiny adoption it seems. | ||||||||
| ▲ | kitd a day ago | parent | prev [-] | |||||||
That's helped by errors.Unwrap() and errors.Is(), though you do need to build and structure your errors appropriately. | ||||||||
| ||||||||