Hi there. Any protips, why is the compiler (1.7.21...
# getting-started
j
Hi there. Any protips, why is the compiler (1.7.21, JVM) complaining about “No cast needed”, when it is in fact required, because otherwise there’s `Int`/`Long` ambiguity?
j
Because a short number alone is always considered to be an Int, if you want to explicitly return a Long, you can write
123L
https://kotlinlang.org/docs/numbers.html#literal-constants-for-numbers
e
that documentation doesn't cover everything. a bare literal integer can be either an
Int
or a
Long
this has been an issue with
sumOf
for as long as it has existed, https://youtrack.jetbrains.com/issue/KT-46360
j
I understand the reason for need to specify Int as return type here, but I don't know why compiler complains when I do it 🙂
e
https://youtrack.jetbrains.com/issue/KT-50177 the quick fix is wrong (this should be a KTIJ bug since that's where the problem comes from)
as a workaround, you can write
0.toInt()
instead of
as Int
to force the type, but the KTIJ plugin will (falsely) show that as unnecessary too 😞
j
No, the problem exist on gradle build log as well, so it's not IJ specific.
e
hmm I don't see the message in the kotlin compiler source, maybe it's a different one…
j
Copy code
❯ ./gradlew clean build

> Task :compileKotlin
w: /Users/jakub.gwozdz/personal/advent-of-code-2022/src/main/kotlin/day2/Day2.kt: (33, 11): No cast needed
w: /Users/jakub.gwozdz/personal/advent-of-code-2022/src/main/kotlin/day2/Day2.kt: (49, 11): No cast needed
🙂
e
ah, that makes it easy to find then. a
@Suppress("USELESS_CAST")
will stop that from showing up
j
yes that’s what I did, but I was wondered why is it like so. Apparently it’s a known bug/problem, so I’m ok with that 🙂 Thanks for the help and the links
k
In any case, why bother with such code when you can do
lineSequence().count { it == "one" }
?
j
that was just the
foo()
example 🙂