3 ms·
We tried to use Optional<> instead of returning null. Accidentally returning null from methods returning Optional<> is something that actually happens, especial
by gurkendoktor 11y ago
We tried to use Optional<> instead of returning null. Accidentally returning null from methods returning Optional<> is something that actually happens, especially when migrating existing code. Of course you can solve that with @Nonnull annotations, but then why use Optional in the first place?
The other problem is that the Optional<> interface is clunky. There is ifPresent(), but no ifAbsent(). The stream syntax is incompatible with the rest of the Java world, in the same way that List<T>#toArray has always been ridiculous. And if you use ifPresent(), you have to live with the fact that you cannot return from Java lambdas, and you cannot change any variables or even use non-final variables. I want to scratch my eyes out when I see people allocate one-element arrays on the heap just to work around Java's lambdas.
I don't see the value of Optional<T> if you already have @Nullable.
- wcummings 11y ago>if you use ifPresent(), you have to live with the fact that you cannot return from Java lambdas That's what map/flatMap is for, ifPresent is really only useful for side-effects.