Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8140281

(opt) add no-arg orElseThrow() as preferred alternative to get()

    Details

    • Subcomponent:
    • Resolved In Build:
      b36

      Backports

        Description

        Optional.get() is an "attractive nuisance" and is too tempting for programmers, leading to frequent errors. People don't expect a getter to throw an exception. A replacement API for Optional.get() with equivalent semantics should be added. The specification for Optional.get() should direct readers to the new API. However, Optional.get() shouldn't be deprecated just yet; deprecation is covered by the follow-on issue JDK-8160606.

        The current proposal for replacement is a no-arg Optional.orElseThrow(). This fits well within the family of or-else methods, and it's closely related to orElseThrow(exSupplier), as if providing a NoSuchElementException supplier as a default.

          Issue Links

            Activity

            Hide
            smarks Stuart Marks added a comment - - edited
            A previous proposal for replacement is Optional.getWhenPresent(). This name reinforces the assumption the caller is making, namely that the value is present. However, the "when" seemed to connote time, not the notion of definiteness, misleading people into thinking that it did something like blocking until a value was present. Alternatives include getOrThrow() and orElseThrow().

            Previous review thread of proposal that included deprecation of Optional.get():

                http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-April/040484.html
            Show
            smarks Stuart Marks added a comment - - edited A previous proposal for replacement is Optional.getWhenPresent(). This name reinforces the assumption the caller is making, namely that the value is present. However, the "when" seemed to connote time, not the notion of definiteness, misleading people into thinking that it did something like blocking until a value was present. Alternatives include getOrThrow() and orElseThrow(). Previous review thread of proposal that included deprecation of Optional.get():      http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-April/040484.html
            Hide
            smarks Stuart Marks added a comment -
            Show
            smarks Stuart Marks added a comment - Review thread of updated proposal: http://mail.openjdk.java.net/pipermail/core-libs-dev/2017-December/050465.html
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk/jdk/rev/7acf5700d542
            User: smarks
            Date: 2017-12-14 03:23:53 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk/jdk/rev/7acf5700d542 User: smarks Date: 2017-12-14 03:23:53 +0000

              People

              • Assignee:
                smarks Stuart Marks
                Reporter:
                smarks Stuart Marks
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: