Back to Home

Groovy as the best Java

java groovy

Groovy as the best Java

    Groovy can be used in different ways - for scripts, for Grails, for quick prototyping, 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 Groovy-way, you can always write in the way that is customary in Java, and if you remember, here you have Closures, convenient lists, and many other wonderful things .

    The only thing that prevented using Groovy to develop production code was the lack of compilation errors in a large number of cases. For example, if you call a nonexistent method, you refer to a nonexistent variable, etc.

    For many Groovy frameworks and libraries this is really necessary (see for example working with XMLin Groovy), but if I write regular code, it seriously bothers me.

    So, finally in Groovy 2.0 there was an opportunity to say - check the 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 can be on a class or a method), we get errors:

    image

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

    image

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

    A full list of checks can be found here .

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

    Read Next