Kiryushin Andrey
05/15/2020, 10:54 AMit
parameter in lambdas contained in pairs, lists or other data structures. This code sample
var pair: Pair<Int, (String) -> String> = 2 to { "param $it" }
var pair2: Pair<Int, (String) -> String> = 2 to { p -> "param $p" }
var list: List<(String) -> String> = listOf { "param $it" }
var list2: List<(String) -> String> = listOf { p -> "param $p" }
looks fine to IDE, but fails to compile with the following errors for the lines using it
lambda parameter:
Type inference failed. Expected type mismatch: inferred type is Pair<Int, () -> String> but Pair<Int, (String) -> String> was expected
Unresolved reference: it
Type inference failed. Expected type mismatch: inferred type is List<() -> String> but List<(String) -> String> was expected
Unresolved reference: it
But the compiler is happy with the root-level lambda declarations like this
var func: (String) -> String = { "param $it" }
var func2: (String) -> String = { p -> "param $p" }
Is this a bug or an intended behavior? Are there any workarounds that would make the compiler happy and at the same time keep the code neat (that is, using it
but without full lambda type annotations at usage site)?
I'm using Kotlin 1.3.71 in multiplatform project and can see this both in JVM and JS targets.wasyl
05/15/2020, 10:59 AMKiryushin Andrey
05/15/2020, 11:01 AMKiryushin Andrey
05/15/2020, 11:02 AMwasyl
05/15/2020, 11:05 AMKiryushin Andrey
05/15/2020, 11:07 AMit
parameter despite of the expected function type, while the new one takes the expected function type into account and allows using it
.wasyl
05/15/2020, 11:11 AMKiryushin Andrey
05/15/2020, 11:13 AMKiryushin Andrey
05/15/2020, 11:14 AMKiryushin Andrey
05/15/2020, 11:17 AM