David Kubecka
01/18/2024, 10:56 AM@OverloadResolutionByLambdaReturnType
annotation. So while this works as expected (prints 0
)
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
fun test(a: () -> Int) = a() + 1
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
fun test(a: () -> String) = a()
test { "0" }
changing the signature of the second overload to fun test(a: () -> Any)
makes the code no longer compiling (with type mismatch error) . Is this something which can be potentially fixed?David Kubecka
01/18/2024, 10:59 AMAdam S
01/18/2024, 12:12 PMfun interface
provider for each argument type. It's a little more verbose but I think it achieves what you want https://pl.kotl.in/0jrPgiu89David Kubecka
01/18/2024, 12:32 PMfun interface MyStringProvider : MyProvider<Any>
leads to the same issue as in my version.Adam S
01/18/2024, 1:21 PMDavid Kubecka
01/18/2024, 1:50 PM@OverloadResolutionByLambdaReturnType
fun foo(cb: (Unit) -> String) = Unit // (1)
@OverloadResolutionByLambdaReturnType
fun foo(cb: (Unit) -> CharSequence) = Unit // (2)
fun testError02() {
// Error: required String, found CharSequence
foo { a ->
val a: CharSequence = "42"
a
}
// Both (1) and (2) are applicable
// (1) is the only most specific candidate
// We do not attempt refinement by the lambda return type
}
Seems that the current limitation is intended. Probably the compiler would be too complex otherwise.