Hello guys :wave: I was looking through the source...
# android
k
Hello guys 👋 I was looking through the sources of Kotlin and found interesting thing:
0.toInt()
Came down to the annotation class
IntrinsicConstEvaluation
, the docs say it's to run compiler optimization. Maybe someone can tell me why to explicitly cast 0 to Int? @Ivan Kylchik
👍 1
i
Hi! Thanks for reaching out. Let’s start with the question: why is it important to you? Just out of curiosity or there is a problem in such code? I did my research on this. This code is actually generated by stdlib generator, see this piece of code. This one generator method is used for all
sumOf
versions. So I think the answer is that it is just easier this way. We can just write
<http://0.to|0.to>$typeShortName()
and avoid big
when
where we select the exact literal. As you said, this code will be optimized at compile time, so there will not be overhead on runtime (at least for the case with primitives).
k
Thank you very much for the reply! I didn't notice any problems with this code, I just wondered why it was implemented that way 😃