groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul King (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (GROOVY-6744) Have the elvis operator (?:) support the Optional type in Java 8
Date Fri, 09 Jun 2017 00:15:18 GMT

    [ https://issues.apache.org/jira/browse/GROOVY-6744?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16043661#comment-16043661
] 

Paul King edited comment on GROOVY-6744 at 6/9/17 12:14 AM:
------------------------------------------------------------

Marked as a breaking change for anyone relying on the old behavior.

If you were working around the old behavior using the ternary operator, i.e.:
{code}
def empty = Optional.empty()
def foo = Optional.of('foo')
def bar = Optional.of('bar')
assert foo.isPresent() ? foo : bar == foo
assert empty.isPresent() ? empty : bar == bar
{code}
you can leave your code unchanged or change to the more compact elvis operator notation version:
{code}
assert foo ?: bar == foo
assert empty ?: bar == bar
{code}



was (Author: paulk):
Marked as a breaking change for anyone relying on the old behavior.

If you were working around the old behavior using the ternary operator, i.e.:
{code}
def empty = Optional.empty()
def foo = Optional.of('foo')
def bar = Optional.of('bar')
assert foo.isPresent() ? foo : bar == foo
assert empty.isPresent() ? empty : bar == bar
{code}
you can leave your code unchanged or change to the more compact:
{code}
assert foo ?: bar == foo
assert empty ?: bar == bar
{code}


> Have the elvis operator (?:) support the Optional type in Java 8
> ----------------------------------------------------------------
>
>                 Key: GROOVY-6744
>                 URL: https://issues.apache.org/jira/browse/GROOVY-6744
>             Project: Groovy
>          Issue Type: New Feature
>          Components: Compiler
>            Reporter: Aadi Deshpande
>            Assignee: John Wagenleitner
>              Labels: breaking
>             Fix For: 2.5.0-beta-1
>
>
> 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.
> {code:java}
> Optional<String> string1 = Optional.of("foo")
> Optional<String> string2 = Optional.empty()
> assert "foo" == ( string1 ?: "bar" )
> assert "bar" == ( string2 ?: "bar" )
> {code}
> 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.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Mime
View raw message