On 13.01.2018 04:07, Nathan Harvey wrote:
Sure thing. Here's a Java example:
void sample(Function<String, Integer> fn) {
System.out.println("fn");
}
void sample(Supplier<Integer> sp) {
System.out.println("sp");
}
These methods can exist side by side, and are called correctly even in cases
of Lambda, eg:
sample(s -> 123); // fn
sample(() -> 123); // sp
On the other hand, take this Groovy code:
def sample(Function<String, Integer> fn) {
println "fn"
}
def sample(Supplier<Integer> sp) {
println "sp"
}
in theory we can resolve this, because we do know how many parameters
the Closure has and in sample({ s -> 1 }) we know we have one, thus it
should be the Function. What we cannot do with Closure is the following:
interface I1 {
String m(String s)
}
interace I2 {
Integer m(Integer i)
}
def c(I1 i1){}
def c(I2 i2){}
c(i -> i+1)
c(i -> i.toString())
What should not work even in Java is the following:
interface I1 {
String m(String s)
}
interace I2 {
String m(Integer i)
}
def c(I1 i1){}
def c(I2 i2){}
c(i -> (i+1).toString())
c(i -> i.toString())
that is because here the return type will give no additional
information, so only the parameter type can work and i.toString() works
for both, i+1 should in theory also work for both.
bye blackdrag