Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsTo make a Groovy closure act like a DSL, give it a DSL object as its delegate and choose a resolution strategy that defines which receiver handles implicit method and property references. The default is OWNER_FIRST, so the enclosing owner takes precedence over the delegate. For a strict DSL boundary, use DELEGATE_ONLY; describe the expected receiver to IDEs and the type checker with @DelegatesTo.
What this, owner and delegate mean
These three references are related, but they are not interchangeable. In a closure, this refers to the class instance where the closure was defined. owner refers to the enclosing object or, when closures are nested, the enclosing closure. delegate is a configurable object used to resolve implicit property and method references.
Delegation is what lets a DSL closure say send() or to rather than spelling out delegate.send() or delegate.to. The closure’s lookup strategy decides whether the owner or delegate gets the first chance to handle such implicit references. For the full closure semantics, see the Apache Groovy closures guide.
How Groovy resolves implicit calls and properties
Groovy’s default closure strategy is Closure.OWNER_FIRST. For an implicit method or property that is not a local variable, Groovy checks the owner first and then tries the delegate if the owner does not supply it. If both have a member with the same name, the owner’s member wins.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Resolution strategies do not override local-variable lookup: Groovy looks up local variables first regardless of the strategy. A local variable can therefore shadow a name that a DSL author expects to resolve on the delegate.
Choose a closure resolution strategy
| Strategy | Lookup order | When it fits |
|---|---|---|
OWNER_FIRST |
Owner, then delegate. This is the default. | When the closure should retain access to its enclosing context, while still allowing delegate fallback. |
DELEGATE_FIRST |
Delegate, then owner. | When DSL members should take precedence but owner members may still serve as fallback. |
OWNER_ONLY |
Owner only; delegate lookup is ignored. | When implicit references should stay with the enclosing context. |
DELEGATE_ONLY |
Delegate only; owner lookup is ignored. | When the DSL should provide a strict receiver boundary. A missing member will not fall back to the owner. |
TO_SELF |
The closure object itself. | Advanced metaprogramming, including custom Closure subclasses. |
The strategy names and lookup behavior are documented in the Groovy Closure API.
Build a DSL closure with an explicit delegate
A DSL method typically creates the object that defines the DSL, configures the closure to use it as delegate, sets the intended strategy, and then runs the closure. For example, a minimal email-style DSL can look like this:
class EmailSpec {
String to
String subject
void send() {
println "Sending '$subject' to $to"
}
}
def email(@DelegatesTo(strategy = Closure.DELEGATE_ONLY,
value = EmailSpec) Closure<?> body) {
def spec = new EmailSpec()
def configured = body.rehydrate(spec, this, this)
configured.resolveStrategy = Closure.DELEGATE_ONLY
configured()
}
email {
to = 'reader@example.com'
subject = 'Hello'
send()
}
rehydrate returns a closure configured with the supplied delegate, owner and this object. Here, the DSL object is the delegate, and DELEGATE_ONLY makes implicit property assignments and method calls resolve there rather than falling back to the enclosing object. The Apache Groovy DSL guide uses this pattern to explain closure delegation.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallRank #3
The strict strategy also means the delegate must supply every implicit member the closure needs. If a referenced property or method is absent from the delegate, lookup cannot fall back to the owner.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Keep runtime behavior and tooling metadata aligned
@DelegatesTo tells IDEs and Groovy’s type checker which receiver a closure is expected to use. It does not set the closure’s runtime delegate or resolution strategy. The DSL method must configure those itself, and its annotation should describe the same delegate type and strategy as the runtime behavior.
Rank #4
- Used Book in Good Condition
The annotation was introduced in Groovy 2.1; that is its historical introduction version, not a statement about the support status of any particular current Groovy release. Consult the DSL guide for annotation usage.
Quick Recap
Best Value
Decide whether your DSL should fall back
- Use
DELEGATE_FIRSTif DSL names should win but access to owner members remains a deliberate convenience. - Use
DELEGATE_ONLYif accidental access to owner members would make the DSL confusing or unsafe; ensure the delegate exposes all required operations. - Keep
OWNER_FIRSTwhen preserving the enclosing context is more important than giving the DSL priority. - Check for local-variable shadowing when a name unexpectedly resolves somewhere other than the delegate.
- Match
@DelegatesToto the implementation so editor assistance and type checking describe the actual DSL receiver and strategy.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

