Groovy web console

subscribe to the feed Subscribe
to this
site

Leveaging the implicit call() nmethod to do postfix conditionals (fixed)

Published 10 years ago by Tim Yates with tags call dsl conditional
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
class Conditional {
  private fn 
  def run( fn ) {
    this.fn = fn
    this
  }
  def when( cond ) {
    if( cond ) fn()
  }
  def call(obj) { obj }
}

Conditional c = new Conditional()
c.with {
  def a = ""
  run { a = "cool" } when ( 19 > 100 )
  assert a == ""
  run { a = "cool" } when ( 19 > 1 )
  assert a == "cool"
  print a
}