erroneous "redundant lambda arrow" (or problem with Kotlin compiler)

Hi all,

when trying to make a map from a string to a function (for those curious why I wanted to do such a thing , see part 2 of https://adventofcode.com/2015/day/16) , like 

val myMap = mapOf<String, (Int) -> Boolean>(
"cats" to { it == 3}
)

IDEA 2020.1.4 (community edition) gives

 

Type inference failed. Expected type mismatch:
required:
Pair<String, (Int) → Boolean>
found:
Pair<String, () → Boolean>

This problem (not sure why it is a problem, usually { it == 3} is a valid lambda) can be 'solved' by changing the code to 

val myMap = mapOf<String, (Int) -> Boolean>(
"cats" to { it -> it == 3}
)

However, then IDEA gives the warning "Redundant lambda arrow". If I then click "remove the arrow", it cycles back to the first version, with the type mismatch error message.

So I suppose either the first error is incorrect, or the second hint. Or possibly both. I'm not sure whether this bug is known or deemed important enough to solve, but I encountered it so I am listing it here, hoping that either it will be fixed or at least I will get some better insights into the Kotlin language (which I love, by the way!).

0
3 comments
Official comment

Please try the latest EAP version of Kotlin 1.4.0-RC, it should not be a problem: https://kotlinlang.org/eap/#build-details. New type inference is smart enough to figure out the correct type without arrow.

Thank you very much for your reply, Alexey. Downloading the EAP indeed solved the problem! I am very happy with the improvement - and quite impressed too - I've worked with parsing (ANTLR) in the past, and in my experience it can be quite hard to correctly analyze syntax, let alone process it intelligently.

I'm also grateful to the Kotlin team for this improvement! Thank you all!

 

0

Please sign in to leave a comment.