Noticed some odd behavior around resolving functions that I think might be a compiler bug, but maybe I’m just missing some rule that actually makes sense.
If you have two functions that operate on an interface, and a sub-interface of that interface, when you pass a concrete type to this function the kotlin compiler will resolve the function with the more specific type.
However, if this interface takes a type parameter, and the more general overload has a type bound (eg of
Any
), when you pass a value that satisfies the bounds, the compiler suddenly can’t figure out which one to call.
So this works:
fun <T : Any> StateFlow<T>.thing(): Unit = TODO()
fun <T : Any> Flow<T>.thing(): Unit= TODO()
MutableStateFlow("").thing()
And this:
fun <T> StateFlow<T>.thing(): Unit = TODO()
fun <T> Flow<T>.thing(): Unit = TODO()
MutableStateFlow("").thing()
And this:
fun <T : Any> StateFlow<T>.thing(): Unit = TODO()
fun <T> Flow<T>.thing(): Unit= TODO()
MutableStateFlow("").thing()
But not this:
fun <T> StateFlow<T>.thing(): Unit = TODO()
fun <T : Any> Flow<T>.thing(): Unit = TODO()
MutableStateFlow("").thing()