ollaw
ollaw

Reputation: 2213

Kotlin cannot infer type of Function with method reference

why in the example below, with mapTwo Kotlin can't infer the type of a ?

fun <U> mapOne(f: (Int) -> U): U = TODO()
fun <U> mapTwo(f: Function<Int, U>): U = TODO()

fun <T> mapper(a: T): List<T> = TODO()

fun main() {
    mapOne(::mapper)
    mapTwo(::mapper) //won 't compile
}

Upvotes: 1

Views: 227

Answers (1)

IlyaMuravjov
IlyaMuravjov

Reputation: 2502

Type inference is not the cause of a compilation error. mapTwo<List<Int>>(::mapper) will not compile either because of ::mapper having the type (T) -> List<T> (KFunction1<T, List<T>>), which is not assignable to Function<Int, U>.

Upvotes: 1

Related Questions