【发布时间】:2014-04-11 09:24:44
【问题描述】:
这是一个代码示例:
class StaticTest {
static def list = [1, 2, 3]
void printsNothing() {
[].with { list.each { println it } }
}
void printsList() {
new Object().with { list.each { println it } }
}
public static void main(String[] args) {
new StaticTest().with {
println "Expected: "
printsList()
println "Strange: "
printsNothing()
}
}
}
如您所见,printsNothing 和 printsList 中的闭包是相同的,但结果不同,因为 printsNothing 确实没有打印任何内容,就好像 list 是空的一样。这是输出:
Expected:
1
2
3
Strange:
我正在使用启用了调用动态支持的 Groovy 2.2.2。
关于这是一个错误还是我对 Groovy 一无所知的任何建议?
【问题讨论】: