是否可以在Groovy中更改方法引用闭包上的委托?

时间:2017-10-26 08:30:03

标签: groovy

我一直在试验闭包和委托范围。我已经成功地更改了“常规闭包”的委托,但是在通过.&运算符将方法转换为闭包时却没有:

class Mother {
  def method2() { nonexisting } 
  Closure method3 = {-> nonexisting}
}

Mother julia = new Mother()

// works for regular closures
def c3 = julia.method3
c3.delegate = [nonexisting: 'nonexisting']
assert 'nonexisting' == c3.call() // 'nonexisting' comes from the delegate Map


// fails for method reference closure
def c2 = julia.&method2
c2.delegate == [nonexisting: 'nonexisting']
println "this = ${c2.thisObject}"
println "owner = ${c2.owner}"
println "delegate = ${c2.delegate}"
assert 'nonexisting' == c2.call() // raises MissingPropertyException

我做错了还是不可能让方法引用闭包在我选择的对象中查找自由变量?

0 个答案:

没有答案
相关问题