When / why do I want to use Groovy @CompileStatic?

I read this: http://docs.groovy-lang.org/latest/html/gapi/groovy/transform/CompileStatic.html and this: Should I use Groovy @CompileStatic if I also use Java 7 and understand that, of course, there are performance improvements, but is that so? I do not understand what @CompileStatic does @CompileStatic .

Are there specific classes where adding @CompileStatic ? Where I do not want this?

+7
annotations groovy
source share
1 answer

To quote my part of my answer on Should I use Groovy @CompileStatic if I also use Java 7 :

While Groovy is faster than usual, it can only compile a subset of Groovy and behaves a little differently. Especially all dynamic functions are not available.

All MOPs will be bypassed. Builders will not work in general, some of them have extensions for the compiler to allow them to pass. Methods are also selected at compile time using static types, and Groovy typically uses methods available at runtime and runtime types. This can cause various methods to be called.

Of course, @CompileStatic also provides some security, since the compiler’s job is to check programs at run time. But since static information is doomed to be incomplete, there can never be 100% security.

So, where is this not a problem ... well ... POGO, for example, since they usually do not contain all this code. And, of course, for classes ported from Java to Groovy using copy & paste.

Where do i want Well, it’s probably on Android at the moment, since there the code size has an effect, and the static compiled code is more compact. Otherwise, I personally am well versed in not using @CompileStatic at all. It is rather a matter of taste. In some cases, there is a performance improvement for tight loops, but this requires you to transition and identify by first profiling your application.

+11
source share

All Articles