What does .delegate mean in groovy?

Language/Groovy 2016. 9. 8. 15:55


50down voteaccepted

The delegate of a closure is an object that is used to resolve references that cannot be resolved within the body of the closure itself. If your example was written like this instead:

def say = {
  def m = 'hello'
  println m
}
say.delegate = [m:2]
say()

It prints 'hello', because m can be resolved within the closure. However, when m is not defined within the closure,

def say = {
  println m
}
say.delegate = [m:2]
say()

the delegate is used to resolve the reference, and in this case the delegate is a Map that maps mto 2.

출처 - http://stackoverflow.com/questions/8120949/what-does-delegate-mean-in-groovy


'Language > Groovy' 카테고리의 다른 글

Metaprogramming  (0) 2016.09.09
그루비의 동적 객체지향 - 1  (0) 2016.09.09
그루비의 동적 객체지향 - 2  (0) 2016.09.08
ExpandoMetaClass  (0) 2016.09.08
Groovy - 문법 (Java 와 비교해서 다른 점 위주로)  (0) 2016.09.05
: