I get Ambiguous error
when I try to use a code which contains overloaded methods with functional arguments.
I wrote a small snippet which shows an ambiguous behavior:
import java.util.function.BiConsumer
import java.util.function.Consumer
class Test {
static void main(String... args) {
execute({ x -> println("Consumer") })
execute({ x, y -> println("BiConsumer") })
}
static void execute(Consumer<Integer> c) {
c.accept(100)
}
static void execute(BiConsumer<Integer, Integer> c) {
c.accept(1, 2)
}
}
Output (2.4.9 groovy):
Exception in thread "main" groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method Test#execute.
Cannot resolve which method to invoke for [class Test$_main_closure1] due to overlapping prototypes between:
[interface java.util.function.BiConsumer]
[interface java.util.function.Consumer]
at groovy.lang.MetaClassImpl.chooseMostSpecificParams(MetaClassImpl.java:3268)
at groovy.lang.MetaClassImpl.chooseMethodInternal(MetaClassImpl.java:3221)
at groovy.lang.MetaClassImpl.chooseMethod(MetaClassImpl.java:3164)
at groovy.lang.MetaClassImpl.pickStaticMethod(MetaClassImpl.java:1516)
at groovy.lang.MetaClassImpl.retrieveStaticMethod(MetaClassImpl.java:1412)
at org.codehaus.groovy.vmplugin.v7.Selector$MethodSelector.chooseMeta(Selector.java:553)
at org.codehaus.groovy.vmplugin.v7.Selector$MethodSelector.setCallSiteTarget(Selector.java:954)
at org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterface.java:228)
at Test.main(Test.groovy:7)
But it works with java-lambdas and I don't understand how to use a groovy-closures in this situation.
You need to put as XXX
after your closures to give the runtime a clue where you want to go
execute({ x -> println("Consumer") } as Consumer)
execute({ x, y -> println("BiConsumer") } as BiConsumer)
Should do it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With