📜 ⬆️ ⬇️

Groovy like best java

Groovy can be used in different ways - for scripts, for Grails, to quickly write prototypes, for DSL, etc.

Groovy has always attracted me as improved Java. In fact, almost any Java code will be Groovy valid code — i.e. if you don't remember how to do something in the Groovy-way, you can always write like it is in Java, and if you remember, here are Closures, handy lists, and many other wonderful things .

The only thing that prevented Groovy from developing production code was the absence of compilation errors in a large number of cases. For example, if you call a nonexistent method, you refer to a non-existent variable, etc.
')
For many Groovy frameworks and libraries, this is really necessary (see, for example, working with Groovy XML ), but if I write plain code, it seriously bothers me.

So, finally, in Groovy 2.0, it is possible to say - check types in this class, the existence of methods and variables!


Take, for example, this class:

image

It compiles without problems.

But if we add
@TypeChecked (this annotation may be on a class or method), then we get errors:

image

Also, the check is automatically enabled if we @CompileStatic static compilation for the class with the @CompileStatic annotation:

image

Now you can write to Groovy everything that was written in Java and not be afraid of problems missed by the compiler.

A full list of checks can be found here .

It would be cool if they did the default type checking, and the dynamic typing by annotation ...

Source: https://habr.com/ru/post/145363/


All Articles