Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

I misspelled a method call, but it sill compiled. What gives?

Take this simple script as an example:

Code Block
titleGreet.groovy
class Greet {
  def salute( person ) { println "Hello ${person.name}!" }
  def welcome( Place location ) { println "Welcome to ${location.state}!" }
}

g = new Greet()
g.salude()                //misspelling
g.welcome( 123 )          //wrong argument type

...

Note that running groovyc Greet.groovy does not produce any errors. Instead, a MissingMethodException is thrown at that lineruntime.

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.

...