2 ms·
I despaired of the Java process' ability to fix this kind of issue when I saw what a hash they made of the Optional implementation (not allowing Optional to con
by lmm 4y ago
I despaired of the Java process' ability to fix this kind of issue when I saw what a hash they made of the Optional implementation (not allowing Optional to contain null undermines most of its advantages, and destroys all incentive for migrating to Optional if your codebase isn't going to reach 100% null-free in the forseeable future; not to mention the (explicitly stated!) desire to "not be a monad" i.e. to have wilfully idiosyncronatic behaviour).
Trying to make this a not-type is doomed to failure. It's a property of expressions, it has rules about how it flows through expressions; that means it's a type, or it's an ad-hoc informally specified implementation of half a type. It's recapitulating the mistake of checked exceptions all over again.
- deleted 4y ago[deleted]
- kaba0 4y agoCould you expand on the “not a monad” part? Java’s type system with generics is surprisingly expressive, but it can’t have higher kinded types, so a real Monad interface is not possible as far as I know. But it does have methods to use it as you would use a Maybe in Haskell, so I don’t find its occasional use problematics at all. As for checked exceptions, I disagree that is a mistake. They correspond one-to-one with Result types, but are just better integrated in the platform (has proper stack traces, bubble up as default, and auto-unwrap). Java’s implementation has much to be desired (it didn’t have sum types back then), but checked exceptions themselves should get a reevaluation.
- lmm 4y ago> Could you expand on the “not a monad” part? Java’s type system with generics is surprisingly expressive, but it can’t have higher kinded types, so a real Monad interface is not possible as far as I know. But it does have methods to use it as you would use a Maybe in Haskell People in the design mailing list discussion argued that those methods shouldn't be added because they didn't want Optional to be a monad. > They correspond one-to-one with Result types They don't, because throwing them is not a value (there is no way to have a variable that you can substitute for "throw e" and get the same behaviour), and because they're not properly integrated with the language's type system. Throws-ness is a separate parallel universe that behaves almost but not quite the same as expression type. Which is exactly what this is proposing to do with nullness.
- kaba0 4y agoThe way Optional is/could at the time be implemented is far from performant, so they probably just didn’t want to overrely on such an abstraction. Even intellij’s default linter will call out on you if you use it as a class field for example. While I do like more pure FP languages very much, I don’t think there is all that much value in everything being a value besides some mathematical elegance. Besides, a throwing exception can be turned into a value via a try-catch block, and can be just as easily tested as if it were a value, so where is the downside? Exceptions are for exceptional situations, result types can still be used for very much expected ones. E.g. a primitive file read operation returning either a byte or EOF is great as a result type. Some specific IOException is better modeled as an exception which you might not be interested in handling at that level. With all that said, I’m interested in the newer generation of languages with effect types that might bridge this gap.
- lmm 4y ago> While I do like more pure FP languages very much, I don’t think there is all that much value in everything being a value besides some mathematical elegance. Besides, a throwing exception can be turned into a value via a try-catch block, and can be just as easily tested as if it were a value, so where is the downside? It's really cumbersome in practice. E.g. when processing results from a database query (say), you probably want to call some callback for every row and then close the transaction and return the result of that callback. But handling exceptions around that becomes a significant pain. Once you've used a language with proper result types you don't want to go back, IME.
- kaba0 4y agoIf the exception is something you can handle at that function’s scope, just put it inside a try-catch. Otherwise, let it bubble up and let it close the transaction (or however you want to handle that). If anything, I feel it is easier to do with Java than in, say, Haskell though I will add that my experience with Haskell doing db queries is limited.
- 4y ago
- imtringued 4y agoYou can't add nullness to types because that would break generics. List<String!> will still have nullable methods and fields.
- mabbo 4y ago> not allowing Optional to contain null undermines most of its advantages This isn't the first time I've heard this and I'm still not certain I understand. To me, Optionals are an alternative way to say "no value" over the existing way - null. I like Java's Optional because it means I can stop worrying about null. An Optional that wraps a null doesn't give me that. I still need to do null checks! Why would I want that?
- lmm 4y agoOne advantage of Optional is allowing you to remove null from the language and then stop worrying about null checks. But you can't get that in Java, because you can't remove null from the language. So in practice that benefit is very small - you still have to worry about null in Java code, and will for decades. The other advantage of Optional is that it behaves consistently regardless of what's inside it, which means you can use it in generic code in a way that you can't use null. For example a common problem with null in Java is that if you call map.get(key) and the result is null, you can't tell whether that means the key isn't present in the map, or the key is present but the value is null. Whereas if you're using optionals, you can distinguish between None (key not present) and Some(None) (key is present, but mapped to None). So Java could have added a getOptional method to Map (with a default impl), and then if getOptional(key) returns None the key isn't present, if it returns Some(null) then the key is present but mapped to null. That would be a legitimate, immediate use case, that would give people a benefit from switching to Optional today, and would start the ball rolling on using optionals, and maybe eventually in a few decades they would be widespread enough that we could start deprecating null in the language. But instead they made it so you can't put null in Optional from day 1. Which would be great if there was a way to instantly migrate everything, but it makes it harder to start using Optional in an existing codebase and reduces the short-term benefits, for the sake of a future that (because of that very lack of short-term benefits) will never come.