groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andres Almiray <aalmi...@gmail.com>
Subject Re: [PROPOSAL]new operator ?=
Date Wed, 23 Nov 2016 08:54:41 GMT
FWIW

[image: Inline image 1] *aalmiray*
If a shorthand notation for Groovy’s Elvis (?:) + assignment were to be
added, which one would you pick?
22/11/16 15:44 <https://twitter.com/aalmiray/status/801073756693151744>
49 votes so far.

48% in favor of ?=
10% in favor of ||=
20% in favor of ?:=
22% had no clue.

In hindsight the 4th option should have been "no thanks".

On Wed, Nov 23, 2016 at 9:34 AM, Marcin Erdmann <marcin.erdmann@proxerd.pl>
wrote:

> 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