3 ms·
I think it's fair to say that having some sort of syntactically lightweight sum or union type facility makes this way nicer than anything Java ever had -- subcl
by remexre 1y ago
I think it's fair to say that having some sort of syntactically lightweight sum or union type facility makes this way nicer than anything Java ever had -- subclassing isn't really a solution, because you often want something like:
type FooError = YoureHoldingItWrong | FileError
type BarError = YoureHoldingItWrong | NetworkError
fn foo() -> Result<int, FooError> { ... }
fn bar() -> Result<int, BarError> { ... }
fn baz() -> Result<String, BarError> { ... }
TypeScript's type system would hypothetically make this pretty nice if there were a common Result type with compiler support.
Rust needs a bit more boilerplate to declare FooError, but the ? syntax automatically calling into(), and into() being free to rearrange errors it bubbles up really help a lot too.
The big problem with Java's checked exceptions was that you need to list all the exceptions on every function, every time.
- esafak 1y agoJava's sealed interfaces enable typing errors. https://blogs.oracle.com/javamagazine/post/java-sealed-classes-fight-ambiguity https://blogs.oracle.com/javamagazine/post/java-sealed-class...
- keybored 1y agoAlthough syntactically lightweight it is the opposite of.
- esafak 1y agoI agree; Java is constitutionally incapable of being lightweight. I much prefer Typescript's union syntax. I'm glad Python copied it.
- peterashford 1y agorecords are lightweight