3 ms·
that makes sense. i think an important question here is - what does "return null" in a method which returns Optional<T> do? will it return a null Optional<T>, o
by bakhy 11y ago
that makes sense. i think an important question here is - what does "return null" in a method which returns Optional<T> do? will it return a null Optional<T>, or will it return a non-null Optional<T> which is empty?
sorry to rub it in, but Java needs value types. this whole issue, however maybe irrelevant, does not exist in C#.
- KfZ 11y agoIt's a very simple question to answer: "return null" returns null.
- bakhy 11y agothen this abstraction has sprung a serious leak. the article seems to be fully right.
- Latty 11y agoThe point is that wherever that happens, it's an obvious flaw with the method returning the null, and can also be caught by static analysis. It means that in general, the problem is avoided. Yes, it's not ideal, it's still a flaw that should be caught at compile-time, but it's not a big real-world problem, as returning null by accident instead of an option is a pretty rare bug. The much more common bug (you didn't realize no return value was a case), the Option is a good counter to. No, it's not a perfect solution, but it's not useless either.
- bakhy 11y agoi understand, and agree. i would personally, though, avoid adding it, and add attributes for the same purpose instead. just as the article suggests... it achieves the same without introducing a new class, thus also remaining backwards compatible.