Substitute 'this' invoking a method reference in Groovy

Viewed 880

Invoking a non-static method reference is easy when the reference is obtained from an instance:

class Foo { void funk() { println "okay!" } }
Foo foo = new Foo()
Closure closure = foo.&funk
closure() // okay! is printed

But how to substitite this when the method reference is obtained from a class?

class Foo { void funk() { println "okay!" } }
Foo foo = new Foo()
Closure closure = Foo.&funk
// closure.delegate = foo // not helpful
closure()
// => java.lang.IllegalArgumentException: object is not an instance of declaring class
1 Answers
Related