On 23.11.2016 09:34, Marcin Erdmann wrote:
[...]
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"
well, you could have done this:
def options = [ fit : "max" ]
options.putAll(userOptions)
then there is no show time for elvis
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"
or Cedric's variant:
foo.options.with {
fit ?= "max" //fit = fit ?: "max"
}
Maybe my problem stems more from me not really being happy with ?= as
symbol group. For a ?: b being the short form of a? a : b is pretty
straightforward. Having a reduction of the reduction... well that's
where you can easily loose context. It is then not so easy anymore that
a ?= b is the short form of a = a ? a : b... especially since this is a
pretty special case with rare usages. And going by the usually pattern
this really would have to be a ?:= b, which is just too much ASCII-art
for some here.
I mean something like a += b is kind of special too, but it is also many
times more common than ?=, because calculations are a much more common
case than after-the-fact-initialization
[...]
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.
Maybe we should start a vote of ?= or not... maybe on the user list...
then we see what people think. Daniel might have been a bit too fast here
bye Jochen