Groovy 클로저를 마크 업으로 허용하는 빌더를 만듭니다. 그러나 중첩 된 클로저를 사용하여 메서드 호출을 잡는 데 문제가 있습니다.Groovy에서 중첩 된 클로저를 통해 델리게이트 전달
Closure nested = {
foo() //will throw missingMethod exception
}
Closure root = {
foo() //prints 'missing foo []'
inline_nested {
foo() //prints 'missing foo []'
}
nested()
}
builder.execute (root)
// ...
class MyBuilder {
void execute (Closure closure) {
def clone = closure.clone()
clone.delegate = this
clone()
}
def missingMethod (String name, args) {
println "missing ${name} ${args}"
}
}
중첩 된 클로저에 대한 대리자 속성을 설정할 수있는 방법이 있습니까?
오른쪽 'include'메서드 호출을 사용하면 훨씬 쉽습니다. – Akusete