Live data from Hacker News

Java 8’s new Optional type doesn't solve anything

medium.com

11–20 of 197 posts

Re: Java 8’s new Optional type doesn't solve anything

#11
some have already commented here that author maybe complains too much (e.g., calling .get() on an Option is a deliberate move, meaning that the developer explicitly took on the risk of that exception. that's very different than implicitly assuming that a regular ref is not null, using it and getting bitten in the ass.). but this one really hurts:

"The last flaw with Java's implementation is a bit ironic — It's possible for the optional reference itself to be null."

ouch.

Re: Java 8’s new Optional type doesn't solve anything

#12
post #4
post #2

TL;DR You can call `Optional.get()` to deliberately circumvent the safety mechanisms of Optional. The author apparently concludes that since it does not solve ALL error cases it does not solve ANY error cases...

The problem is that you don't get guarantees that something is not null; you can still do Optional myOptional = null. Second, you're not required to handle the not-present case. Third, if you attempt to get the value of an optional (using .get()) when it is not present, you get an (unchecked, so no requirement to catch) exception - basically replacing NullPointerException with NoSuchElementException. The only value O…

You can do it today, but that won't work in Java 10 with value types.

Optional is planned to become a value type.

Re: Java 8’s new Optional type doesn't solve anything

#17
post #14

Using get() is just bad style and so is returning null where you could return Collections.emptyList(). Previous discussion on reddit: https://www.reddit.com/r/programming/comments/3pl7o0/java_8s... tl;dr: use map, orElseGet, orElse

Why it's bad? If I'm sure that this optional contains value, I don't see why it's bad. May be I checked this value presence few lines above.

Re: Java 8’s new Optional type doesn't solve anything

#18
The author is missing the point. The fact that Optional can result in a nullpointer doesn't mean you should use in the same manner as null-checks.

You shouldn't replace:

  if(x == null)
  {
    y = x.doSomething();
  }
with

  if(optionalX.isPresent())
  {
    y = x.doSomething();
  }
You should replace it with:

  y = Optional.ofNullable(x)
    .map(ClassX::doSomething)
    .orElse(null);

Re: Java 8’s new Optional type doesn't solve anything

#19
post #5

The lack of pattern matching doesn't make it useless. The existence of Optional reminds the programmer to check whether the value is present, and the type system does enforce this; you can't accidentally treat an Optional as a reference of the same type. The type system does help us remember to handle things; it is a reminder enforced by the type system that's easy to examine during code review. Yes, you can write ge…

Exactly, it forces the programmer to think about whether something can be null or not and actually had a behavioural change in my java coding.
Post reply on HN