I'm just playing with the metaclass programming in Groovy. But suddenly I was facing a little problem which I just could not get working...
Here is the simple script:
// define simple closure
def printValueClosure = {
println "The value is: '$delegate'"
}
String.metaClass.printValueClosure = printValueClosure
// works fine
'variable A'.printValueClosure()
// define as method
def printValueMethod(String s){
println "The value is: '$s'"
}
// how to do this!?
String.metaClass.printValueMethod = this.&printValueMethod(delegate)
'variable B'.printValueMethod()
Is it possible to use the method but set the first parameter to the calling object? using delegate seems not to work... The assignment of methods which do not reference the caller is no problem. Does currying work here?
Thanks, Ingo