As always - it depends. With great power comes great responsibility. Luckily, Groovy also offers solutions if you are looking for some more secure type checking or even static compilation.

If you want to take advantage of Groovy’s dynamic compilation, but you want to improve type checking, you can consider using @groovy.transform.TypeChecked annotation. When we add it to the SomeClass , IDE will mark PLAYERS variable red and say Cannot assign LinkedHashMap<String, List<String>> to Map<String, String> . Also, when we try to compile the class with groovyc , we will end up seeing the following error.

$ groovyc SomeClass.groovy org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: SomeClass.groovy: 6: [Static type checking] - Incompatible generic argument types. Cannot assign java.util.LinkedHashMap <java.lang.String, java.util.List> to: java.util.Map <String, String> @ line 6, column 48. Map<String, String> PLAYERS = [ ^ SomeClass.groovy: 12: [Static type checking] - Cannot assign value of type java.lang.String to variable of type java.util.List <String> @ line 12, column 46. ist<String> footballPlayers = PLAYERS.Fo ^ 2 errors

Alternatively, if you don’t use any of the Groovy’s dynamic features, you can enable static compilation with @groovy.transform.CompileStatic annotation. It enables static type checking and produces the bytecode that is much closer to what Java compiler produces.

Thank you!

Thank you so much for reading up to this point! I hope this article met your expectations, and you have learned something valuable from it. If you have one more minute, leave me a comment below, please. This way, I can thank you for your time more in person.

And last but not least. If you find this blog post valuable, help me spread the word, please. You can share it on Twitter, LinkedIn, or anywhere else you can think of. This single tweet or post on social media helps a lot. One more time - thank you so much! You’re awesome!