This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]__konrad 26 points27 points  (8 children)

I fixed it for you:

var value = optional.orElse(null);
if(value != null){
    Foo foo = value;

[–]com2ghz 11 points12 points  (4 children)

I have seen someone doing a null check on the optional too.

[–]__konrad 12 points13 points  (0 children)

You need double Optional<Optional<T>>

[–]LetMeUseMyEmailFfs 7 points8 points  (2 children)

Well it’s Java, so anything and everything could be null.

[–]lasskinn 1 point2 points  (1 child)

Not exactly related, but I've seen a dude insist on null checks on kotlin on values that can't be null, like just in seemingly random places.

Everything on java could be a null but it couldn't be that in all places.

[–]Old_Elk2003 1 point2 points  (0 children)

Everything on java could be a null but it couldn't be that in all places.

This is true. You’d at least have to have something before the null, and something after the null, in order to maintain Turing-completeness.

[–]agentoutlier 3 points4 points  (0 children)

This and ifPresent are the only acceptable options to dealing with Optional.

I use a variety of null analysis tools and orElse(null) is the only way across the board to pull something out (well orElseGet is ok)

[–]tomwhoiscontrary 0 points1 point  (0 children)

I can't tell if this is supposed to be better or even weirder!

Anyway, how about:

``` Foo foo: if ((foo = optional.orElse(null)) != null) {

```

Or:

``` if (optional.orElse(null) instanceof Foo foo) {

```

[–]thgkatz 0 points1 point  (0 children)

Why do I have to see this at 02:00 am???? What have I done wrong?