2 ms·
But java has the worst of both worlds cause with an optional you now have 3 cases, Optional.empty, !Optional.empty, & null instead of just 2 cases. In our code
by ender341341 4y ago
But java has the worst of both worlds cause with an optional you now have 3 cases, Optional.empty, !Optional.empty, & null instead of just 2 cases.
In our code base this means we have a the rule that Optional is only allowed for returns so we aren't adding 3rd cases all over the place.
- krzyk 4y agoI have never came up to a code that would pass null in place of Optional, why?
- ender341341 4y agoBecause you don't always control where the code is being called from. Other compile time typed languages (C, C++, C#, typescript, rust, kotlin) usually would let you enforce that as part of the type system, but the java compiler will silently accept null in place of optional and throw an NPE at runtime.
- mohaine 4y agoPretty much every JSON parser in java does this by default. Why? Because they were all written before Optional existed. I have to agree with the Optional hate. I loved it in Scala but after a few months on Kotlin I don't think I can go back.
- krzyk 4y agoJackson does it well. Any sane json library has to support newer Java idioms, if not it is not worth using.