nkiesel
04/19/2021, 10:28 PMmapOf("a" to 1L, "b" to 2)
now has a type Map<String, Long>
while it had type Map<String, Any>
in 1.3. This caused a runtime error because the 2
was passed to some Java code which then casts it to an int
. It was easy enough to fix by using `mapOf<String, Any>("a" to 1L, "b" to 2)`but it makes me wonder: could I somehow prevent the automatic promotion of the 2
to a 2L
?Shawn
04/19/2021, 10:32 PMShawn
04/19/2021, 10:32 PMnkiesel
04/19/2021, 10:38 PMval b = 2; val m = mapOf<"a" to 1L, "b" to b); val i = m["b"] as Int
works, but val m = mapOf<"a" to 1L, "b" to 2); val i = m["b"] as Int
says "java.lang.Long cannot be cast to java.lang.Integer". My understanding is that this happens because 2
is a valid literal for a Long
value. I would like to prevent that "promotion" to Long
. Something like 2I
which would make sure the type is Int
and not Long
.nkiesel
04/19/2021, 10:43 PMmapOf("a" to 1L, "b" to 2)["b"] as Int
fails. Thus 2 as Int
will prevent automatic promotion to Long
. I still would think that not accepting 2
as a value for a Long
would be betterRoukanken
04/20/2021, 6:55 AMnkiesel
04/20/2021, 8:44 AMmapOf("a" to 1L, "b" to 2, "c" to true)["b"] as Int
works as well (i.e. 2
stays an Int
and is not promoted to Long
).Tobias Berger
04/20/2021, 9:05 AMnkiesel
04/20/2021, 3:45 PM