streetsofboston
10/16/2020, 2:47 PMclass MyList<T> {
@JvmName("flatMapList")
fun <R> flatMapList(block: Function1<T, List<R>>): MyList<R> = TODO()
@JvmName("flatMapListPair")
fun <R> flatMapList(block: Function1<T, Pair<R, R>>): MyList<R> = TODO()
@JvmName("flatMapList2")
fun <R> flatMapList2(block: (T) -> List<R>): MyList<R> = TODO()
@JvmName("flatMapListPair2")
fun <R> flatMapList2(block: (T) -> Pair<R, R>): MyList<R> = TODO()
}
fun test() {
val x = { it: Int -> Pair("1", "2") }
val y = { it: Int -> listOf("1", "2") }
MyList<Int>().flatMapList(x) // OK
MyList<Int>().flatMapList({ it: Int -> Pair(1, 2) }) // ERROR
MyList<Int>().flatMapList2(x) // OK
MyList<Int>().flatMapList2({ it: Int -> Pair(1, 2) }) // ERROR
MyList<Int>().flatMapList(y) // OK
MyList<Int>().flatMapList({ it: Int -> Pair(1, 2) }) // ERROR
MyList<Int>().flatMapList2(y) // OK
MyList<Int>().flatMapList2({ it: Int -> Pair(1, 2) }) // ERROR
}
Why does the compiler (and IDE) generate an error when directly assigning the lambda, but it compiles fine when using a temporary variable (x
and y
)?Marc Knaup
10/16/2020, 2:52 PM@OverloadResolutionByLambdaReturnType
for whatever reason.
https://youtrack.jetbrains.com/issue/KT-38962#focus=Comments-27-4181061.0-0Nir
10/16/2020, 2:57 PMstreetsofboston
10/16/2020, 3:02 PM