Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Duplicate
-
None
-
None
-
None
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
Attachments
Issue Links
- duplicates
-
GROOVY-5306 [PARROT] Add "a ?= 2" support (Elvis assignment): should be expanded to "a = a == null ? 2 : a"
- Closed
- is duplicated by
-
GROOVY-3880 Optional Assignment Operator
- Closed
- is related to
-
GROOVY-3880 Optional Assignment Operator
- Closed
-
GROOVY-5306 [PARROT] Add "a ?= 2" support (Elvis assignment): should be expanded to "a = a == null ? 2 : a"
- Closed
- relates to
-
GROOVY-4874 Elvis assignment operator
- Closed