【问题标题】:Error when referencing an attribute of type Map inside a closure在闭包内引用 Map 类型的属性时出错
【发布时间】:2016-04-04 04:17:33
【问题描述】:

在下面的代码段中,为什么编译器报错的是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

【问题讨论】:

    标签: groovy closures compile-static


    【解决方案1】:

    貌似CompileStatic注解有bug,好像你把这行改成:

    this.amap += [one:'two']
    

    或者

    this.amap.one = 'two'
    

    然后它工作正常。我猜这是由于 [] 地图访问器的语义。

    你可以sumbit it as a bug看看能不能修复

    【讨论】:

      猜你喜欢
      • 2019-08-27
      • 2016-03-19
      • 2020-10-05
      • 2023-04-08
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多