i have a setter method: `fromJson(JsonToObjectMapp...
# announcements
t
i have a setter method:
fromJson(JsonToObjectMapper jsonToObjectMapper)
(java), which configures how my library does json-mapping:
Copy code
public interface JsonToObjectMapper {
    <T> T map(String json, Class<T> targetClass);
}
calling this method works fine from java, but kotlin is unable to infer types. if i try to to:
Copy code
val gson = GsonBuilder().create()
myPlugin.fromJson(gson::fromJson)
i just get
None of the following functions can be called with the arguments supplied.
any ideas?
a
@tipsy
JsonToObjectMapper
is a kotlin interface, right?
t
i've tried both java and kotlin, java gets me bit farther (the one i pasted here is java)
a
if you define the interface in java, you can try
myPlugin.fromJson { json, cls -> gson.fromJson(json, cls) }
i don't think SAM conversion works with
gson::fromJson
t
yeah, and you can't have lambdas with generics
😞
a
does
JsonToObjectMapper
only have a single method?
t
yes
a
you could use a function type instead of an interface:
Copy code
typealias JsonToObjectMapper<T> = (String, Class<T>) -> T

fun MyPlugin.fromJson(map: JsonToObjectMapper<T>) {
    ....
}
then you can use any function that has the same shape, e. g.
myPlugin.fromJson(gson::fromJson)
works
t
i believe this will work fine from kotlin, but not java
a
it should work just fine (if
kotlin-stdlib
is included in the java project), function types are just functional interfaces
t
alright, i'll give it a try, thanks
Copy code
Type inference failed: Not enough information to infer parameter T in 

fun <T> fromJson(map: JsonToObjectMapper<T> /* = (String, Class<T>) → T */): Unit
worked in java though, so that's good