concept type checking in category groovy

appears as: type checking
Groovy in Action, Second Edition

This is an excerpt from Manning's book Groovy in Action, Second Edition.

The use of the category format makes extension modules very appealing because it’s a common pattern to find utility classes in the form of static methods. Commons Lang is just one example. Extension modules have an interesting advantage compared to categories: while the latter are totally dynamic, the former are statically bound (they’re known when Groovy initializes), making them compatible with type checking and static compilation (see chapter 10 for details).

So in short: the dynamic capabilities of Groovy disallow type checking. But type checking can be very useful to spot typos like in the following listing.

Likewise, it’s definitely not advisable to declare a field or a property with def, especially because we’re living in a multithreaded world. Let’s take a look at the following listing, which exhibits a bad coding style in the context of type checking.

Listing 10.10. Properties shouldn’t be declared with def for type checking

Categories and metaclasses aren’t the only features of Groovy that aren’t compatible with type checking and also why we don’t believe applying @TypeChecked on your whole Groovy codebase makes much sense: if you do, this means that you explicitly decide not to use half of the features of the language, including some that are in particular very powerful: builders.