You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2017/06/09 00:14:18 UTC
[jira] [Comment Edited] (GROOVY-6744) Have the elvis operator (?:)
support the Optional type in Java 8
[ 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:13 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:
{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.
> 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)