Gilles Barbier
02/03/2024, 10:45 AMfun main() {
myFun(I::class.java, I::myMethod, true)
}
fun <P1, P2, R> myFun(
klass: Class<P1>,
method: (P1, P2) -> R,
result: R
) {
println("ok")
}
interface I {
fun myMethod(a: String): Int
}
Joffrey
02/03/2024, 10:52 AMR
isn't constrained. So the compiler can choose that R==Any?
(In the most extreme case), and so any method return type for the second argument will be ok with any 3rd argument type.
If you pass myMethod
from I
which returns Int
, it's still valid if the compiler decides that R==Any
, because Int
is a subtype of any. And Boolean
is also a subtype of Any
Joffrey
02/03/2024, 10:54 AMmyFun
using angles brackets, which would force the conpiler's hand: myFun<.., .., SpecificTypeHere>(...)
Joffrey
02/03/2024, 10:54 AMGilles Barbier
02/03/2024, 10:55 AMJoffrey
02/03/2024, 10:59 AMInt
is Any
no matter what. Do you have a more concrete real life use case where you need this? In general this hasn't turned out to be a problem for me, because usually R
appears somewhere else that the called cares about, like a return value. If this is inferred to Any
, they won't be able to do anything with it anyway, unless it's enough for them, on which case there is no problemGilles Barbier
02/03/2024, 11:05 AM