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

Method chain replacement for Optional.ifPresentOrElse()

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: 14
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      A DESCRIPTION OF THE PROBLEM :
      Optional's new ifPresentOrElse() method is an improvement over the previous if/else idioms used for Optional, but it is somewhat clunky to declare your "ifPresent" logic after the language "orElse". E.g.,

      optional.ifPresentOrElse(System.out::println, System.out::println);

      If, instead, you could chain off the result of ifPresent(), it would result in much more fluid code:

      optional.ifPresent(System.out::println).orElse(System.out::println);

      This could be accomplished by making ifPresent() return itself and adding an orElse() method to optional, or by adding a new return type to ifPresent() specifically for chaining.


        Attachments

          Activity

            People

            • Assignee:
              psandoz Paul Sandoz
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: