Tianyu Zhu
06/12/2021, 6:32 PMval myMap = mapOf("id" to 10, "hash" to 99999L)
I would expect the id
key to contain an Int
and the hash
key to contain a Long
, but that's not the case-- id
is now mapped to a long!
I can fix this by writing
val myMap = mapOf("id" to 10 as Int, "hash" to 9999L)
But then IntelliJ tells me that the cast is not needed.
Is there a better way?hho
06/12/2021, 6:46 PMval myMap = mapOf<String,Number>("id" to 10, "hash" to 99999L)
Tianyu Zhu
06/12/2021, 6:57 PMmap<String, Any>
But I find it very odd that Kotlin does this kind of upcasting. 😞ephemient
06/12/2021, 7:00 PMephemient
06/12/2021, 7:00 PMval i: Long = 10
, val b: Byte = 10
Tianyu Zhu
06/12/2021, 7:01 PMephemient
06/12/2021, 7:02 PMmapOf("id" to 10.toInt(), "hash" to 9999L)
to force it to be considered as an Int
literalephemient
06/12/2021, 7:04 PM<literal>.toInt()
etc. are treated like constant literals by the compiler)Tianyu Zhu
06/12/2021, 7:05 PM