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

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -