RFR(s): 8140281 add no-arg Optional.orElseThrow() as preferred alternative to get()

Brian Burkhalter brian.burkhalter at oracle.com
Fri Dec 8 01:47:30 UTC 2017


On Dec 7, 2017, at 4:33 PM, Stuart Marks <stuart.marks at oracle.com> wrote:

> Please review this changeset that introduces a new no-arg method orElseThrow() to Optional, as a preferred alternative to the get() method.

Looks OK to me.

> Corresponding methods are also added to OptionalDouble, Int, and Long.
> 
> The orElseThrow() method is functionally identical to get(). It has some affinity with the existing orElseThrow(exceptionSupplier) method, being equivalent to orElseThrow(NoSuchElementException::new).
> 
> The get() method is functionally unchanged. It is NOT being deprecated, although some wording in the doc has been added to point to orElseThrow() as the "preferred alternative." This is part of a (slow) migration away from Optional.get(), which has an obvious, attractive name that is often misused. These issues have been discussed on this list previously:
> 
>    http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-April/040484.html

Quite a discussion. Good thing you opted not to name it “Optional.dudeCallIsPresentBeforeCallingMe().”

Brian


More information about the core-libs-dev mailing list