Groovy DSL - luojunqiang/code_snippet GitHub Wiki

Groovy DSL

Delegate

def closure = {
  clear()
  set MONTH, JULY
  set DATE, 4
  set YEAR, 1776
  println time
}
def calendar = Calendar.instance
closure.delegate = calendar
closure()

nearly same as

def calendar = Calendar.instance
calendar.with {
  clear()
  set MONTH, JULY
  set DATE, 4
  set YEAR, 1776
  println time
}

Each Groovy closure has a resolveStrategy associated with it. This property determines how/if the delegate comes in to play. The 4 possible values for the resolveStrategy are OWNER_FIRST, DELEGATE_FIRST, OWNER_ONLY and DELEGATE_ONLY (all constants defined in groovy.lang.Closure). The default is OWNER_FIRST. Consider the owner to be the "this" wherever the closure is defined.

⚠️ **GitHub.com Fallback** ⚠️