Is there a way to resolve the "Overload resolution...
# getting-started
v
Is there a way to resolve the "Overload resolution ambiguity" without having dedicated function names?
Copy code
fun <T : Any> foo(block: () -> T?) {}
fun <T : Any> foo(block: () -> List<T>?) {}
fun <K : Any, V : Any> foo(block: () -> Map<K, V>?) {}
fun <T : Any> foo(block: () -> Set<T>?) {}
foo { "foo" }
foo { listOf("foo") }
foo { mapOf("foo" to "bar") }
foo { setOf("foo") }
(And without casting the arguments, I mean that the right method is used automatically)
r
There's a
@OverloadResolutionByLambdaReturnType
annotation that might help. What do you expect
foo { null }
to resolve to?
It might not help in this case. Since your first generic function is always valid, I'm not sure you can avoid either a resolution ambiguity or a declaration clash.
v
Ah, thanks, that helps indeed. The
null
case is fine to cast. This works:
Copy code
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
fun <T : Any> foo(block: () -> T?) {}
@JvmName("bar")
fun <T : Any> foo(block: () -> List<T>?) {}
@JvmName("baz")
fun <K : Any, V : Any> foo(block: () -> Map<K, V>?) {}
@JvmName("bam")
fun <T : Any> foo(block: () -> Set<T>?) {}
foo { null as String? }
foo { "foo" }
foo { listOf("foo") }
foo { mapOf("foo" to "bar") }
foo { setOf("foo") }
Grml not in all cases, Kotlin Compiler too old (don't ask)
NEW_INFERENCE_IS_LAMBDA_FOR_OVERLOAD_RESOLUTION_INLINE called with null value
😞