2 ms·
I think part of the problem is that Java’s Optional isn’t a Either[null, T] but a Either[null, boxed T, boxed null] ‘Boxed null’ is what the documentati
by Someone 4y ago
I think part of the problem is that Java’s Optional isn’t a
Either[null, T]
but a
Either[null, boxed T, boxed null]
‘Boxed null’ is what the documentation (https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html https://docs.oracle.com/javase/8/docs/api/java/util/Optional...) calls “an empty Optional”
That means that, for example, an Optional[Byte] can have 258 different values and cannot, in general, be compiled to a ”pointer to byte” because that has only 257 different values.
Edit: reading https://news.ycombinator.com/item?id=35133241 https://news.ycombinator.com/item?id=35133241, the plan is to change that. I fear that, by the time they get around to that, lots of code will handle the cases null and Optional containing null differently, making that a breaking change.
- adrianmsmith 4y ago> I fear that, by the time they get around to that, lots of code will handle the cases null and Optional containing null differently, making that a breaking change. Yes, I was working with code once which wrapped string IDs into a FooId object (a good idea in principle) and all of the following had different meanings: FooId x = null; FooId x = new FooId(null); FooId x = new FooId(""); FooId x = new FooId(...an actual ID...) I think one was for not showing any content at all, one was for showing default content, another was that content was there but the user wasn't allowed to see it, etc. I'm so glad I left that company...
- marginalia_nu 4y ago[True|False|FileNotFound] ;-) For what it's worth, Java's also got this class: https://docs.oracle.com/javase/8/docs/api/java/util/OptionalLong.html https://docs.oracle.com/javase/8/docs/api/java/util/Optional... Although in practice there isn't much performance difference in my experience.
- moonchild 4y ago> reading https://news.ycombinator.com/item?id=35133241 https://news.ycombinator.com/item?id=35133241, the plan is to change that. I fear that, by the time they get around to that, lots of code will handle the cases null and Optional containing null differently, making that a breaking change The post your link links to explains exactly how they intend to avoid this problem.
- Someone 4y agoThe way I read that is that it says they’ll introduce an “inline class” that’s used to implement the “reference class” Optional, not that it will be replaced by it. IMO, that’s not solving the problem, but doing the best you can once you’ve decided to implement Optional as a reference class now and as a value class at some future time. I think I would have waited for the proper implementation.
- nayuki 4y agoHow is boxed null possible? The documentation for Optional.of(T value) says that it'll throw NullPointerException if value == null. https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T- https://docs.oracle.com/javase/8/docs/api/java/util/Optional...
- vanjajaja1 4y agoThat's because Optional.of(null) == Optional.empty() Optional.empty() is boxed null
- conro1108 4y agoOptional.of(null) throws a NullPointerException ;) Optional.ofNullable(null) == Optional.empty()
- vanjajaja1 4y agoI'm trying to say that .of(null) and .empty() semantically the same, which is why it throws NPE to force explicit empty. ofNullable is a whole other kettle of fish.