开发者

Will groovy (grails) give you compile time checking like java?

Will groovy (grails) give you compil开发者_运维知识库e time checking like java?

If you always specify the type, will that change things much?


Since Groovy 2.0 it is possible to check types at compile time. Annotations can be used on class or method: @TypeChecked or @CompileStatic. The 1st one tells compiler to do type checking during compilation and the 2nd one force it to do 'static compilation' which more or less results with binary code like from native JAVA compiler. Of course, usage of the 2nd annotation involves type checking by itself.

Grails 2.2 is the first release that uses Groovy 2.0.


The Groovy compiler will of course find syntax errors, but almost no type errors as with Java. Even the use of undeclared variables will often pass compilation.

Declaring types will increase the amount of checking that is done, but not by much.

This is because Groovy is a dynamic language with powerful metaprogramming features that make it impossible for the compiler to know e.g. what methods or fields a given object will have at compile time, since it's possible for this to be changed at runtime by other code.

However, IDE plugins offer a compromise by marking members of variables with a declared type that are not present in the class declaration as possible errors. Then the developer can decide whether this member will be present at runtime, or whether he just made a typo. Additionally, known class members appear in autocompletion.


Things are changing... http://portal.sliderocket.com/vmware/Groovy-2-0-Static-Type-Checking-and-Compilation


Groovy 2.0 has @CompileStatic and @TypeChecked you can check this good performance test between Groovy 2.0 and Java http://java.dzone.com/articles/groovy-20-performance-compared

As topr said... this feature will be avaliable in grails until version 2.2


No.

It was designed to be a dynamic-typing programming languages which is pretty much the opposite as compile time checking.

You'll have to add more unit tests instead.


You can't write unit tests if you use Groovy with script syntax. That's like writing unit tests for shell scripts. Still, you have a compile available, it just doesn't tell you that Sustem.out.println() is nonsense. A compiler option should be available to catch these errors. Not every script contains meta-programming and DSL code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜