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

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

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.5.0-beta-1
    • Compiler

    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

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

              Dates

                Created:
                Updated:
                Resolved: