【问题标题】:Groovy static compilation (@CompileStatic) turns types of class fields into `Object`Groovy 静态编译 (@CompileStatic) 将类字段的类型转换为 `Object`
【发布时间】:2017-12-11 13:35:57
【问题描述】:

编译以下 Groovy 类

@CompileStatic
class StaticCompileTest {

   private def fieldInt = 3

}

反编译.class文件时,该类的结果:

public class StaticCompileTest implements GroovyObject {
    private Object fieldInt;
}

将字段更改为final 不会改变情况。

为什么在这种情况下 Groovy 编译器无法派生类型?

【问题讨论】:

    标签: groovy static-compilation


    【解决方案1】:

    因为假设类型不会改变本质上是不安全的。

    http://docs.groovy-lang.org/latest/html/documentation/#_variables_vs_fields_in_type_inference

    【讨论】:

    • 这是解释我的问题的完美链接,尤其是。因为它也处理final 的情况。谢谢。
    猜你喜欢
    • 2019-06-22
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2010-11-19
    • 1970-01-01
    相关资源
    最近更新 更多