hallvard
01/04/2019, 1:18 PMvar f: Long = 5
, and in the course of my program, the value changes. And then I would like this check: if (f == 10000) { ... }
. Why is it that it doesn't compile? Why do I need to supply an L to the hard-coded constant 10000
here? Shouldn't it be possible for the compiler to detect that 10000
is valid as a Long value?Shawn
01/04/2019, 1:30 PMLong
value, but it’ll still fail .equals()
. I don’t know enough about numeric types on the JVM to really explain why, but I think this is an instance of the compiler disallowing a comparison to avoid subtle bugsShawn
01/04/2019, 1:30 PM5L.equals(5)
which evaluated to false
Shawn
01/04/2019, 1:33 PMLong.equals(Any?)
defined, so it makes sense that the method would include a other is Long
checkShawn
01/04/2019, 1:35 PM==
is supposed to resolve to an equals()
call in Kotlin, but it wouldn’t be useful here and perhaps harmful to the developer to allow it. If implicit widening were allowed, you’d have Int values autoconverted to Long that suddenly wouldn’t be able to be compared to ints and it wouldn’t be obvious from the code whyhallvard
01/04/2019, 2:15 PMilya.gorbunov
01/04/2019, 8:17 PM