I created a code sample that shows the problem I am facing:
class BindingExample { public static void main(String[] args) { Closure closure1 = { printit.call("Hello from closure 1") } Closure closure2 = { printit("Hello from closure 2") } Closure printit = { s -> println("printing: "+s) } Binding binding = new Binding() binding.setVariable("printit", printit) closure1.delegate = binding closure2.delegate = binding closure1()
Printit is a Closure that documentation points to the use of doCall and therefore can be called in short form with ().
However, when this closure becomes available through binding to the delegate, only a long version of the call is allowed. Output:
printing: Hello from closure 1 Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: groovy.lang.Binding.printit() is applicable for argument types: (java.lang.String) values: [Hello from closure 2]
Can someone explain why this is so? If possible, I would also like to see how to make the short work work. I was able to get it working by defining printit as the correct static method (and not a closure), but this will not work for my case, because I really need printit to get some data that is available only inside the method scope (not included in the example, so how my question relates to the binding itself).
source share