7 ms·
Exceptions have implied control flow which makes them strictly worse than the Result types which are, as their name suggests, just types. Imagine if some other
by tialaramex 4y ago
Exceptions have implied control flow which makes them strictly worse than the Result types which are, as their name suggests, just types.
Imagine if some other common types had unrelated features like this baked into them. Want a string? Sorry in my new language all the strings need their own separate thread for some reason.
Actually sorry, I forgot, we're in a Java topic, Java actually did have features baked into types. All of Java's user defined types insisted on living on the heap. Just want to make a pair of integers? Sorry, that's an Object now and so it lives on the heap, whereas just one integer is fine, those are just local stack variables. Did they fix that yet?
- mixedCase 4y agoChecked exceptions are explicit. They are no weirder than doing do-notation in an Either monad, the syntax could be more ergonomic but they are clear as day.
- rotifer 4y agoRegarding user-defined types, probably what you're looking for is JEP 401: Primitive Classes (Preview) [1]. I believe it's currently being worked on. [1] https://openjdk.org/jeps/401 https://openjdk.org/jeps/401
- coldtea 4y ago>Exceptions have implied control flow which makes them strictly worse than the Result types which are, as their name suggests, just types. If they're checked exceptions, then the control flow is hardly "implied". If anything, it's explicit: this method potentially throws X, so if X is raised, expect this control flow consequence.
- woodruffw 4y agoI think the "implied" part is in the callsite ambiguity: a Java method marked with "throws X" can raise X at any callsite in its method body, whereas a Rust function of type `Result<...>` has each `Err` variant marked either directly with a return or with the `?` sugar.
- twic 4y agoThe ideal is probably in the middle. Being able to see where exceptions can be thrown is very helpful if you're manipulating state which can be seen from outside the method, because you need to make sure you leave it in a valid state if an exception is thrown. But in a pure function it's mostly noise.
- saagarjha 4y ago> Exceptions have implied control flow which makes them strictly worse than the Result types which are, as their name suggests, just types. There’s nothing wrong with implied control flow. > All of Java's user defined types insisted on living on the heap. Just want to make a pair of integers? Sorry, that's an Object now and so it lives on the heap, whereas just one integer is fine, those are just local stack variables. That’s not how Java works. The runtime is free to store things wherever it likes, with whatever representation it likes. It needs to follow the semantics of the standard, but whatever gets executed is what the JIT felt like emitting.
- deleted 4y ago[deleted]