Why == generates compilation error on different types in Kotlin
The following code cannot be compiled:
if ("2" == 3) {
//do something
}
Error: Kotlin: Operator '==' cannot be applied to 'String' and 'Int'
However, Kotlin reports that this is structural equality and is translated to a?.equals(b) ?: (b === null)
: https://kotlinlang.org/docs/reference/equality.html .
So what's going on here? How it works?
+3
oshai
source
to share
1 answer
this is a Kotlin bug reported as KT-4071 in youtrack, but a priority Major
, so I think this will be fixed quickly. eg:
val ok = "2".equals(3) // works fine
val error = "2" == 3 // compilation error
0
holi-java
source
to share