Hello, I have a question regarding generics.
I would like to store lambdas into a Map. And those lambdas have a generic parameter.
So, I did this simple code
typealias AFunction<R, A> = suspend (args: A) -> Result<R>
val functionMap = mutableMapOf<String, AFunction<Any?, Any?>>()
fun <R, A> addFunctionToMap(name: String, function: AFunction<R, A>) {
functionMap[name] = function
}
For some reasons, it doesn’t compile (on the line
functionMap[name] = function
) as it is saying it was expecting a type
AFunction<Any?, Any?>
and not
AFunction<R, A>
.
I tried modifying a bit the
addFunctionToMap
function like this:
fun <R: Any?, A: Any?> addFunctionToMap(name: String, function: AFunction<R, A>) {
functionMap[name] = function
}
But, it doesn’t change the compiler error.
Do you have an idea how I could solve that?
🙏