| import java.util.Optional; | |
| import java.util.function.Function; | |
| class Main { | |
| public static void main(String[] args) { | |
| Optional<String> o = Optional.of("foo"); | |
| Function<String, String> f = s -> null; | |
| Function<String, String> g = String::valueOf; | |
| //lawless lolz | |
| System.out.println( o.map(g.compose(f)) ); //Optional[null] | |
| System.out.println( o.map(f).map(g) ); //Optional.empty | |
| //This is because | |
| System.out.println( o.map(f) ); //Optional.empty | |
| //Once again for clarity | |
| System.out.println( o.map(s -> null) ); //Optional.empty | |
| //And now for more lolz | |
| System.out.println( o.flatMap(s -> null) ); //throws NullPointerException | |
| } | |
| } |
This comment has been minimized.
This comment has been minimized.
|
just for clarity? |
This comment has been minimized.
This comment has been minimized.
|
That's correct. Presumably one "advantage" for explicitly disallowing |
This comment has been minimized.
This comment has been minimized.
|
FWIW, we tried this (failed) experiment about 12 years ago. The correct solution is to assume that |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Just for clarity, the result of line 11 is an
Optionalof the String"null", not anOptionalofnull.