Runtime vs Compile time, Static vs Dynamic
I misspelled a method call, but it sill compiled. What gives?
Note that running
groovyc Greet.groovy does not produce any errors. Instead, a
MissingMethodException is thrown at that line.
This is because Groovy is a dynamic language. Several other things could be happening to make this code valid at runtime. Using the MetaClass, you could add a
salude() method to the Greet class at runtime. You could also add a
state property to
Number, which would make the
welcome(..) call valid. See ExpandoMetaClass and Groovy Categories.
Will I get a performance boost if I statically type all of my fields and methods?
Actually, no. The way Groovy method selection is done, it actually takes longer if you provide lots of static type information. This could possibly change in the future, but as of Groovy 1.1 this is not the case. See this thread for more info.
Can't we provide some sort of "NotDynamic" flag to speed up method invocation?
This has been discussed on the mailing list many times before. Although it has not been ruled out entirely, there are many implications to such a 'feature'. The primary focus right now is to improve the performance of normal, dynamic Groovy. Groovy is already performant enough for "most" use cases. If you see a performance bottleneck in your code, the typical optimization path is to factor out those pieces to Java code, and call them from Groovy. See this thread and this one.