Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. “null is used in java to represent uninitialized fields”
- I thought this is what constructors are for, isn’t it?
- So either you can initialize a field in the constructor or it has a meaning in your domain to
- leave that field empty and in this 2nd case it should be represented with an Optional.
- 2. “It is not possible to write a method that takes either an X or an Optional”
- These methods clearly have 2 different semantics and then it is correct that they are 2
- totally separated method. In the first case you pass a value of type X that for sure is
- there, in the second a value of type X that could be also possibly empty. The fact that
- you have to distinguish these cases forces you to correctly reason on them.
- 3. “Callers of your method are forced to unpack the optional immediately”
- You have no idea of how the map and flatMap methods work, right? ;)
- 4. “Any pre-existing API that uses null to signal no-value”
- “Absence of a signal should never be used as a signal” – J. Bigelow, 1947
- 5. “For example, java.util.Map has a get() method, and if anything ought to return an
- Optional, surely that should! Unfortunately, it doesn’t and it can’t be made to do so”
- Optional.ofNullable(map.get(key));
- it isn’t that difficult in the end ;)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement