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/05/02 02:03:09 UTC

[jira] [Closed] (GROOVY-5306) Add "a ?= 2" support: should be expanded to "a = a == null ? 2 : a"

     [ https://issues.apache.org/jira/browse/GROOVY-5306?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Paul King closed GROOVY-5306.
-----------------------------

> Add "a ?= 2" support: should be expanded to "a = a == null ? 2 : a"
> -------------------------------------------------------------------
>
>                 Key: GROOVY-5306
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5306
>             Project: Groovy
>          Issue Type: Improvement
>          Components: syntax
>            Reporter: Rodrigo Rosenfeld Rosas
>            Assignee: Daniel Sun
>             Fix For: 3.0
>
>
> I've suggested last week the creation of the "?:=" constructor in Groovy:
> https://jira.codehaus.org/browse/GROOVY-5291
> It was rejected and a new JIRA was requested to be created with a new operator.
> Then, I've talked to the Grails users in their mailing list to get some feedback, which can be read here:
> http://grails.1312388.n4.nabble.com/Help-improving-Groovy-syntax-tt4384137.html
> Then, Phil DeJarnett has suggested using "?=" instead of "?:=". Not only I preferred this suggestion, but I was changed my mind about its meaning too.
> It would be used as a caching/memoization operator mostly. It would be similar to Ruby's "||=", except for this specific situation:
> Ruby:
> {code}
> a = nil
> a ||= false # a will be false
> a ||= true # a will be true
> {code}
> That is why "a ||= value" is expanded to "a = a || value"
> But for caching/memoization, I'd prefer "a ?= value" to be expanded to "a = a == null ? value : a". This way we would have:
> Proposed Groovy syntax:
> {code}
> def a = null
> a ?= false; assert a == false
> a ?= true; assert a == true
> a = null; a ?= new Object(); assert a instanceof Object
> {code}
> I'll actually fill a new ticket on Ruby Redmine too for proposing the same syntax and semanthics :)



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