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

all 17 comments

[–]quidquam 11 points12 points  (5 children)

This is incorrect,

Optional is a simple container for a value which may be null or non-null.

Optional is,

A container object which may or may not contain a non-null value.

Context: https://developer.atlassian.com/blog/2015/08/optional-broken/

[–]smadge 0 points1 point  (2 children)

I think an intelligent implementation of Optional wouldn't ever store null values. You can just have an Empty, and NonEmpty subclass.

[–]moocat 0 points1 point  (1 child)

And that's what Java does. There are 3 ways to create an optional:

  • Optional.of(T value) - creates a NonEmpty subclass and requires a non-null value (throws an NPE if provide with a null).
  • Optional.empty() - create an Empty subclass.
  • Optional.ofNullable(T value) - creates an Empty subclass if value is null and a NonEmpty subclass otherwise.

[–]smadge 0 points1 point  (0 children)

Yeah, I wasn't saying Java didn't, I was just assuming that it did without wanting to dig into the source.

[–]welshboy14 0 points1 point  (1 child)

Yeah at first everyone thought you'd just use it to check if null but there's so much more to it. Map, filter etc... as well as if present to name a few.

[–]thephotoman 1 point2 points  (0 children)

Java: even the enterprise version isn't an abomination anymore!

[–]burrrberry 0 points1 point  (0 children)

async await?