Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Groovy's ternary conditional operator support statements, not just expressions?

Is it possible to include statements with expressions with Groovy's conditional operator? This is what I'm doing now, and I want break this down in a single conditional statement with the println statements...

if(!expired){
  println 'expired is null'
  return true
}
else if( now.after(expired)){
  println 'cache has expired'
  return true
}
else
  return false

Converted into single statement...

return (!expired) ? true : (now.after(expired)) ? true : false

...would like to do something like this for debugging purposes...

return (!expired) ? println 'expired is null' true : (now.after(expired)) ? println 'cache has expired' true : false
like image 825
raffian Avatar asked Dec 16 '22 18:12

raffian


1 Answers

As GrailsGuy said in the other answer, use closures:

def expired= false, expired2= true
return (!expired) ? 
  {println "expired is null"; true}() :
  (expired2) ? {println "cache has expired"; true}() : false
like image 132
Vorg van Geir Avatar answered May 13 '23 15:05

Vorg van Geir