Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6744

Have the elvis operator (?:) support the Optional type in Java 8

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.5.0-beta-1
    • Component/s: Compiler
    • Labels:

      Description

      With Java 8 and the rising popularity of Optional type in lieu of null management, it would be great if Groovy's elvis could handle Optional in the same way.

      Optional<String> string1 = Optional.of("foo")
      Optional<String> string2 = Optional.empty()
      
      assert "foo" == ( string1 ?: "bar" )
      assert "bar" == ( string2 ?: "bar" )
      
      

      Conversely, another option is to make elvis overloadable. ( something like orElse() ).

      That way, other Option/al types ( i.e. from Guava or FJ ) can have extension methods to support their use as well.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                jwagenleitner John Wagenleitner
                Reporter:
                aadi.deshpande Aadi Deshpande
              • Votes:
                1 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: