groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marcin Erdmann <marcin.erdm...@proxerd.pl>
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)
    options.fit = options.fit ?: "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:

    foo.options.fit = foo.options.fit ?: "max"

vs.

    foo.options.fit ?= "max"

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

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

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.

Cheers,
Marcin



--
View this message in context: http://groovy.329449.n5.nabble.com/PROPOSAL-new-operator-tp5736886p5736916.html
Sent from the Groovy Dev mailing list archive at Nabble.com.

Mime
View raw message