You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Daniel Sun (JIRA)" <ji...@apache.org> on 2017/01/25 08:57:26 UTC
[jira] [Closed] (GROOVY-5291) Add "a ?:= 2" support: should be
expanded to "a = a ?: 2"
[ https://issues.apache.org/jira/browse/GROOVY-5291?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Daniel Sun closed GROOVY-5291.
------------------------------
Resolution: Duplicate
Assignee: Daniel Sun
Fix Version/s: (was: 4.0)
3.0
> Add "a ?:= 2" support: should be expanded to "a = a ?: 2"
> ---------------------------------------------------------
>
> Key: GROOVY-5291
> URL: https://issues.apache.org/jira/browse/GROOVY-5291
> Project: Groovy
> Issue Type: Improvement
> Components: syntax
> Reporter: Rodrigo Rosenfeld Rosas
> Assignee: Daniel Sun
> Fix For: 3.0
>
>
> This would allow the very common useful idiom found in Ruby:
> {noformat}
> variable ||= value
> {noformat}
> Currently we have to write code like this in Groovy:
> {noformat}
> def myMap = [:]
> if (condition) (myMap[someLongNameObject.getKeyId()] = myMap[someLongNameObject.getKeyId()] ?: []) << someNewValue
> {noformat}
> Also, if getKeyId() is costly or shouldn't get called more than once for some reason, we would have to write it like:
> {noformat}
> def myMap = [:]
> if (condition) {
> def key = someLongNameObject.getKeyId()
> (myMap[key] = myMap[key] ?: []) << someNewValue
> }
> {noformat}
> When it could be simplified as this:
> {noformat}
> if (condition) (myMap[someLongNameObject.getKeyId()] ?:= []) << someNewValue
> {noformat}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)