groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marcin Erdmann <>
Subject Re: [PROPOSAL]new operator ?=
Date Wed, 23 Nov 2016 08:34:35 GMT
Jochen Theodorou wrote
> So for me a new operator makes more sense. But frankly...
>> def foo(x) {
>>   return x ?: "empty"
>> }
> or even
>> def foo(x) {
>>   x = x ?: "empty"
>>   return x
>> }
> vs.
>> def foo(x) {
>>   x ?= "empty"
>>   return x
>> }
> Is that really worth it? Does it really improve readability that much? 
> Or maybe someone has a better example?

I will chip in as the person who proposed that new operator on Twitter to
Daniel (thanks Daniel and Guillaume for raising it for discussion here).
This idea came up when I needed to add a default key in a map pre-populated
with another map:

    def options = [:]
    options.putAll(userOptions) = ?: "max"

I think that from the above you can see that this operator makes more sense
when you are defaulting a value that is nested and not just a local
variable. To make it even more drastic: = ?: "max"

vs. ?= "max"

I now see that I could actually simplify the first example in my email to:

    def options = [fit: "max"]

but this is not the first time that I wish this operator existed.

Anyway, I see that it looks like Daniel has decided that it's not worth the
effort but thanks to everybody for taking my proposal into consideration.


View this message in context:
Sent from the Groovy Dev mailing list archive at

View raw message