4 ms·
Well, I think that's why a lot of people are disappointed that Go doesn't quite have sum types, because sum types do increase expressivity around values in gene
by vacuity 3y ago
Well, I think that's why a lot of people are disappointed that Go doesn't quite have sum types, because sum types do increase expressivity around values in general. That they improve error handling is just one application.
- randomdata 3y agoThere seems to be no consensus as to what sum types actually means, but the most popular definition I find in my travels is: Tagged unions. Which is funny as a sum is not a union, but anyway... Assuming you share in that definition, while Go might benefit from tagged unions in general, I'm not sure they actually help in any way with remember to handle errors. A union is just as easy to forget to handle as a single discriminate type. But perhaps you are of another sum type school? Perhaps one of the other sum type definitions are useful here?
- vacuity 3y agoTagged union is a good definition. I was thinking of Rust's Result type, which has Ok(some value) and Err(some value) variants.
- randomdata 3y agoRight. See that doesn't help, at least not in the context of Go without any other modifications. Logically, you can reduce that Result state to a simple boolean (Ok = true, Err = false). But the bool type in Go is prone to the very same problem. It doesn't matter how many type layers you add. The problem is that there is nothing in the language that enforces handling of values, no matter what type that value might be.
- vacuity 3y agoYes, manual tagging doesn't provide type safety. That's why I said some people are disappointed that Go doesn't have sum types. There isn't even a convoluted workaround either, at least not a full one.
- randomdata 3y agoIt is not so much a matter of lack of type safety, it is forgetting to implement an application requirement. Even an advanced type system capable of formal proofs isn't going to help you if you've forgotten the requirement when defining the types. And if you are using a language with a lesser type system (Go, Rust, any language you are likely to use in the real word), and haven't forgotten the requirement, then you would have encoded that knowledge in tests instead, making the forgetfulness in implementation ultimately immaterial. After all, there is no practical difference between learning that you forgot to implement logic by the compiler telling you or the test suite telling you. So, we can infer that the OP is talking about where one straight up forgot about the requirement from start to finish. I'm skeptical that there is any technical solution to that problem, but sum types most certainly isn't it.
- vacuity 3y agoThere is a point at which even dependent typing won't save you, but sum types are a useful tool to reduce negligence. Don't let perfect be the enemy of good.
- randomdata 3y agoSum types are useful tool, but they are not helpful in this particular case. There is nothing about sum types that would help avoid this problem in the slightest. Perfect need not be the enemy of good, but good is not equivalent to not even trying. This is not a general "wouldn't it be nice of Go had..." thread. We are talking about a specific problem.
- vacuity 3y agoI'm not really sure what your point is then. We should have comprehensive tests and therefore error checking isn't special compared to other programming mistakes? I think we should both have tests (don't skimp on them if given time, of course) and use tools like sum types to write more robust programs from the get-go. Errors can even be treated like other values using sum types; that raises the bar universally instead of just improving error checking.