在闭包中引用 Map 类型的属性时出错
Error when referencing an attribute of type Map inside a closure
在下面的代码段中,为什么编译器会报错 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
闭包内应该引用封闭 class 的实例。
注:Groovy版本:2.4.5
看起来像 CompileStatic
注释中的错误,就像您将行更改为:
this.amap += [one:'two']
或
this.amap.one = 'two'
然后它工作正常。我猜这是由于 []
地图访问器的语义。
您可以sumbit it as a bug看看是否可以修复
在下面的代码段中,为什么编译器会报错 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
闭包内应该引用封闭 class 的实例。
注:Groovy版本:2.4.5
看起来像 CompileStatic
注释中的错误,就像您将行更改为:
this.amap += [one:'two']
或
this.amap.one = 'two'
然后它工作正常。我猜这是由于 []
地图访问器的语义。
您可以sumbit it as a bug看看是否可以修复