gpt4 book ai didi

groovy - 我何时/为什么要使用 Groovy 的 @CompileStatic?

转载 作者:行者123 更新时间:2023-12-02 00:45:36 25 4
gpt4 key购买 nike

我读过这个:http://docs.groovy-lang.org/latest/html/gapi/groovy/transform/CompileStatic.html ,这个:Should I use Groovy's @CompileStatic if I'm also using Java 7 ,并了解肯定会有性能改进,但真的是这样吗?我不太明白 @CompileStatic 的作用。

是否有某些类可以轻松添加 @CompileStatic ?我哪里不想要它?

最佳答案

引用我对 Should I use Groovy's @CompileStatic if I'm also using Java 7 的回答的一部分:

While faster than normal Groovy, it can compile only a subset of Groovy and behaves a bit different. Especially all the dynamic features are not available anymore.

所有 MOP 将被绕过。构建器通常无法工作,有些构建器对编译器进行了扩展以允许它们通过。此外,方法是在编译时使用静态类型选择的,而 Groovy 通常使用运行时可用的方法和运行时类型。这可能会导致调用不同的方法。

当然@CompileStatic也提供了一些安全性,因为编译器的任务是在运行时验证程序。但由于静态信息注定是不完整的,因此永远不可能有 100% 的安全。

那么,哪里是显而易见的……嗯……例如 POGO,因为它们通常不包含那么多代码。当然还有通过复制和粘贴从 Java 移植到 Groovy 的类。

我想要它在哪里?嗯,目前可能在 Android 上,因为代码大小会产生影响,并且静态编译的代码更加紧凑。否则我个人完全不使用@CompileStatic 就很好。这更多是一个品味问题。在某些情况下,紧密循环的性能会得到提高,但这需要您首先通过分析应用程序来进行识别

关于groovy - 我何时/为什么要使用 Groovy 的 @CompileStatic?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29609130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com