samedi 17 octobre 2015

Pass param with .& operator to a static method

With groovy .& operator one can create references to static methods, as in

def static xyz( name='Joe' ) {
 println "Hello ${name}"
}

// NOTE: ConsoleScript number part varies
def ref = ConsoleScript52.&xyz

And can be easilly called without params, as

ref()  // prints "Hello "

But how can this method be called with params? ref('John') gives an error groovy.lang.MissingMethodException: No signature of method: ConsoleScript52.xyz() is applicable for argument types: (java.lang.String) values: [John]

Note that Groovy does not even use the default value of name param in above example when static method is called with ref.





Aucun commentaire:

Enregistrer un commentaire