Details
Description
This would allow the very common useful idiom found in Ruby:
variable ||= value
Currently we have to write code like this in Groovy:
def myMap = [:] if (condition) (myMap[someLongNameObject.getKeyId()] = myMap[someLongNameObject.getKeyId()] ?: []) << someNewValue
Also, if getKeyId() is costly or shouldn't get called more than once for some reason, we would have to write it like:
def myMap = [:]
if (condition) {
def key = someLongNameObject.getKeyId()
(myMap[key] = myMap[key] ?: []) << someNewValue
}
When it could be simplified as this:
if (condition) (myMap[someLongNameObject.getKeyId()] ?:= []) << someNewValue
Issue Links
- is related to
-
GROOVY-5306
Add "a ?= 2" support: should be expanded to "a = a == null ? 2 : a"
-
Cedric, Guillaume and myself find all don't like this approach. While we want to be able to make Groovy programs we don't want it to become too much like Perl. the step from a=a?:2 to a=a?a:2 is small, but from a?:=2 to a=a?:2 I think is too much. Maybe with a different syntax - in another jira entry