Groovy Closing a short call method does not work when binding through a delegate?

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() //This works fine closure2() //This does not. //Why does .call() work and () alone not? Most documentation says they're the same. } } 

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).

+4
source share
1 answer

As for WHY, this is so, I can not give a definite answer, unfortunately. There, some talk about the implicit annotation "this", etc. It seems like this should work, but that there is some kind of uncertainty as to which to try first (this-scope or delegate).

That the problem exists currently seems correct. I found the following other resources that agree with some discussions without permission on why.

Discuss the problem: http://groovy.329449.n5.nabble.com/Binding-Closure-property-not-called-as-method-td5562137.html

JIRA Ticket Received: http://jira.codehaus.org/browse/GROOVY-5367

+2
source

Source: https://habr.com/ru/post/1468752/


All Articles