Hey folks, I was wondering if there is any Keep or...
# random
d
Hey folks, I was wondering if there is any Keep or discussion about something that I would call “partial inference on type parameters” Example:
Copy code
interface Parent
class Child : Parent

fun <P : Parent, R> P.foo(): R? {
    return this as? R
}

fun test() {
    // First type param is completely unnecessary and redundant
    Child().foo<Child, Int>()
    
    // This compiles too, as all the types are inferred. Everything or nothing :)
    val i: Int? = Child().foo()
}
Common scenario: From stdlib:
Copy code
public inline fun <reified R> Iterable<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> {
    return filterIsInstanceTo(ArrayList<R>())
There is a big problem here: to avoid declaring two params, we have unbounded
R
. I recently did a small refactor of untested code (ye, untested code is already bad per se..), but it was just a matter of changing
A
with
B
in a few places, and what could possibly go wrong? Kotlin is a strongly typed language, right? But probably not in this case:
Copy code
class A
class B

val list: List<A>
list.filterIsInstance<B>() // 💥
y
Well, you can do
Child().<_, Int>()
but it's definitely ugly. I wish there was a way to specify that certain type params should always be inferred.
d
Yes, indeed I was wondering if there is a Keep, a feature request, or whatever related to that 🙂
y
You might get more interest/info on #CQ3GFJTU1
d
Thank you! I’ll move it there then 🙂
h
This is the reason I never use filterIsInstance but a plain mapNotNull. I ran into this error a few times too and it always took a while why the resulting list was empty.