Earlier quoted context omitted.
> Java’s exceptions are pretty much the exact same sum type as in Rust, just with added syntactic sugar. Except Java doesn’t actually return a result object that can be assigned to a variable by default.
And in case of exceptions, why would that be useful? (For other things, I agree that sum types are cool, and fortunately are supported in Java through sealed classes)
The Trouble with Checked Exceptions (2003)
31–35 of 35 posts
Re: The Trouble with Checked Exceptions (2003)
#32Earlier quoted context omitted.
And in case of exceptions, why would that be useful? (For other things, I agree that sum types are cool, and fortunately are supported in Java through sealed classes)
I don’t think sealed classes offer exhaustive pattern matching. Instead you have an if-else ladder of instanceof checks. This is not the same as most union type implementations.
Re: The Trouble with Checked Exceptions (2003)
#33> Bill Venners: But aren't you breaking their code in that case anyway, even in a language without checked exceptions? If the new version of foo is going to throw a new exception that clients should think about handling, isn't their code broken just by the fact that they didn't expect that exception when they wrote the code? > Anders Hejlsberg: No, because in a lot of cases, people don't care. They're not going to ha…
Re: The Trouble with Checked Exceptions (2003)
#34An old but goodie. While Hejlsberg touches on a fair amount of good points in this discussion, I’ve never agreed with the end result. He went on to make Typescript - making sure that JS has types, so when making method calls you could have the compiler tell you when you did something wrong. Again, errors/exceptions did not get a throw clause, making it impossible to model the error state that the language provides. Y…
I think the idea is that exceptions are too hard to handle without going too far from JS, so you're supposed to handle errors Go-style, but instead of [result, error] you can return result | error thanks to union types. TS is here to statically type JS, but it can't check for exceptions, so pushing people to use them would greatly reduce the value of the tool.
I've said this in another comment, but I really think sum/union types can give you 90% of the benefits of checked exceptions. In the article, `FileNotFoundException` is mentionned. That's something that could be handle with the function returning File | FileNotFound. You can achieve this with sealed classes in C# and Java, but it sounds painful to do this every time. Like someone else said, with checked exceptions you have ad-hoc unions, instead of having to create them yourself with sealed class. Since you have neither sum types, nor unions in Java or C#, checked exceptions in Java are your best tool to handle your case.
Re: The Trouble with Checked Exceptions (2003)
#35Earlier quoted context omitted.
But in languages supporting this you do know that it's either A or B and you can use match expressions to deal with both cases. Usually these are the languages with more focus on type safety, like OCaml, Haskell and Rust.
To be fair, in OCaml, Haskell or Rust the type "A | B" also had to be declared beforehand.