Is it possible to have a list and use it as an argument for the closure signature, and instead multiple variables? The reason is that I have to cause closure from java code, and Java code will not know what variables are required to close groovy.
This is best used with an example.
Say I have a “closure store” where each closure can have different signatures. EG:
closures = [ closureA: { int a, String b -> a.times { System.err.println(b); } }, closureB: { int a, int b, String c -> (a+b).times { System.err.println(c); } } ]
Then I have a method that I am exposing for my java code to trigger these closures:
def tryClosureExpansion(String whichClosure, Object ... args) { def c = closures[whichClosure] c.call(args) // DOESNT COMPILE ! }
And this is Java, I would call this method as follows:
// these calls will happen from Java, not from Groovy tryClosureExpansion("closureA", 1, "Hello"); tryClosureExpansion("closureB", 1, 5, "Hello more");
See above on a line that does not compile. It seems to me that groovy is 'groovy' is enough to handle something like that. Any alternative that can fly?
source share