https://kotlinlang.org logo
#eap
Title
z

Zac Sweers

12/23/2020, 9:18 PM
Type inference in 1.4.30-M1 seems to have regressed significantly with generic bounds. Filed here https://youtrack.jetbrains.com/issue/KT-44045
d

dmitriy.novozhilov

12/24/2020, 8:06 AM
cc @Victor Petukhov
v

Victor Petukhov

12/24/2020, 8:45 AM
Unfortunately, earlier it was a mistake to infer a type parameter to its upper bound without any other constraints (earlier the error didn’t appear by accident due to redundant incorporation of constraints). In inference, we don’t consider constraints to come from upper bounds as proper constraints in order to infer a type parameter only on their basis. You need to always have some other kind of explicit type source to infer a type parameter: value argument, explicit type argument, expected type, and so on.
Copy code
interface Foo

fun <T : Foo> foo(x: () -> T) {}
fun <T> materialize(): T = TODO()

fun <T : Foo> bar(): T {}

fun testing() {
    val x = foo(materialize()) // "Not enough information to infer parameter T" should be reported too
    val y = bar() // "Not enough information to infer parameter T" is already reported
}
Please use some explicit source of type to infer a type parameter. For materialization-like functions you can specify an expected type or type argument:
Copy code
fun testing() {
    val y: Foo = bar() // OK
    // or
    val z = bar<Foo>() // OK
}
z

Zac Sweers

12/24/2020, 8:49 AM
I see, thanks for the detailed explanation 👍
2 Views