Details

    • Subcomponent:
    • Understanding:
      Fix Understood

      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

            People

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

              Dates

              • Created:
                Updated: