groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ocs@ocs.cz" <...@ocs.cz>
Subject null-based Elvis?
Date Wed, 14 Dec 2016 18:04:08 GMT
Hello there,

perhaps it's just me, but I keep bumping into code like

foo.bar.bax!=null?foo.bar.bax:whatever

Note that it cannot be replaced by an Elvis, for it would eat up falses (like numeric zeroes,
empty strings etc.)

In my personal opinion it would be nice to have a null-based Elvis for that.

I am not sure what operator could be used, perhaps ??: -- or can anyone see something better?

The functionality should be self-evident -- if the left side is non-null, it is the result
(even if a false!) Otherwise the right side:

1??:2 == 1
0??:1 == 0 // unlike 0?:1 == 1
null??:1 == 1
'hi'??:'there' == 'hi'
''??:'val' == '' // unlike ''??:'val' == 'val'

etc.

All the best,
OC


Mime
View raw message