Groovy ifs - how to do it better (rewritting code from Java to Groovy) -
let's assume have such code in java:
if(a) { return x } if(b) { return y } return z
how rewrite in groovy?
we can't write because isn't working. can write this:
if(a) { return x } else { if(b) return y else return z }
but isn't quite elegant. ideas?
it can done ternary operator:
def x = 1 def y = 2 def z = 3 def = null def b = 1 assert 2 == ? x : b ? y : z = 1 b = null assert 1 == ? x : b ? y : z = null b = null assert 3 == ? x : b ? y : z
Comments
Post a Comment