在下面的代码段中,为什么编译器会提示 map 属性而不是其他类型的属性:
import groovy.transform.CompileStatic
@CompileStatic
class TestMapInClosure {
Map amap = [:]
List alist = []
Integer intval = 0
Closure doFoo = {
this.amap['one'] = 'two' // !! [Static type checking] - No such property
this.alist.push(1)
this.intval += 5
}
}
this
在闭包中应该引用封闭类的实例,如果我理解正确的话。
注意:Groovy 版本:2.4.5
最佳答案
看起来像 CompileStatic
注释中的错误,就像您将行更改为:
this.amap += [one:'two']
或者
this.amap.one = 'two'
然后它工作正常。我猜这是由于 []
map 访问器的语义所致。
你可以 sumbit it as a bug看看能不能修好
关于groovy - 在闭包中引用 Map 类型的属性时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36394497/