Hi! Anyone knows why Kotlin can’t invoke Java meth...
# server
b
Hi! Anyone knows why Kotlin can’t invoke Java method with
Function
parameters?
v
What does the error say if you hover over the red-underlined part?
b
image.png
v
Works fine here in Kotlin REPL
Copy code
val f: java.util.function.Function<String, String> = java.util.function.Function { it }
 foo.Main.from(f)
res2: kotlin.collections.(Mutable)Map<kotlin.String!, kotlin.String!>! = {}
Ah, interesting, with CacheLoader it does not work
I tried before with
Copy code
public static <K, V> Map<K, V> from(Function<K, V> function) {
        return emptyMap();
    }
b
Yes. It seems to be an interoperability issue.
v
I had that method in a Java class
m
Are you using the Function class from guava or the java.util?
👍 2
b
From
java.util.function
, you can see that at the top of the source code.
v
Ah, the
from
method expects a Guava Function though, not a Java Function
This works fine:
Copy code
com.google.common.cache.CacheLoader.from<String, String> { it }
As well as this:
Copy code
val f: com.google.common.base.Function<String, String> = com.google.common.base.Function { it }
com.google.common.cache.CacheLoader.from(f)
b
Ah, thanks! Stupid question!
v
Stupid same-named interfaces
e
well the Guava one came first. IIRC they were going to transition to the Java interfaces, but I haven't been keeping track of where that is