Groovy 在闭包内引用Map类型的属性时出错
在以下代码段中,为什么编译器抱怨map属性而不是其他类型的属性:Groovy 在闭包内引用Map类型的属性时出错,groovy,closures,compile-static,Groovy,Closures,Compile Static,在以下代码段中,为什么编译器抱怨map属性而不是其他类型的属性: import groovy.transform.CompileStatic @CompileStatic class TestMapInClosure { Map amap = [:] List alist = [] Integer intval = 0 Closure doFoo = { this.amap['one'] = 'two' // !! [
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
}
}
如果我理解正确,闭包中的这个
应该是指封闭类的实例
注意:Groovy版本:2.4.5看起来像是
CompileStatic
注释中的一个bug,好像您将行更改为:
this.amap += [one:'two']
或
那么它工作得很好。我猜这是由于[]
映射访问器的语义造成的
你可以去看看能不能修好
this.amap.one = 'two'