I’m wondering if there is any way how Kotlin can infer the return type of the function based on its lambda parameter. I have the following code in Java:
private Map<Class<?>, Object> map = new HashMap<>();
private <T> T getInstance(Class<T> clazz) {
Object value = map.get(clazz);
return (T)value;
}
public <T, R> R process(Class<T> clazz, Function<T,R> f) {
T instance = getInstance(clazz);
return f.apply(instance);
}
@Test
public void testFoo() {
map.put(Integer.class, 42);
double doubleValue = process(Integer.class, x -> x + 13.2);
System.out.println("Got double result "+doubleValue);
}
This works nicely and I’m trying to recreate a similar thing in Kotlin. This is what I have so far
var map = mapOf<KClass<out Any>, Any>().toMutableMap()
fun <T : Any> getInstance(clazz : KClass<in T>) : T {
val instance = map[clazz]
if (clazz.isInstance(instance)) {
return instance as T
} else {
throw IllegalArgumentException("No instance for $clazz")
}
}
inline fun <reified T : Any, R> process( noinline f : (T) -> R): R {
val instance = getInstance(T::class)
return f(instance)
}
class Test {
@Test
fun test() {
map.put(Integer::class, 42)
val doubleValue = process<Integer, Double> { it.toDouble() + 13.2 }
println("Got double result $doubleValue")
}
}
Now this works great, but I would like to make it even better - ideally I would like to omit the requirement to specify the output type in the process function - so instead of writing
val doubleValue = process<Integer,Double> { it.toDouble() + 13.2 }
I would like to write just
val doubleValue = process<Integer> { it.toDouble() + 13.2}
and infer the return type from the return type of the lambda itself. Is this doable? Thoughts? Ideas?