Anyone else been caught a couple times by Kotlin's...
# advent-of-code
n
Anyone else been caught a couple times by Kotlin's 32 bit integer default? smh
8
p
I've switched all my solutions to long
n
I started to do it, but it can get annoying fast. e.g. I wrote my own
sumBy
which returns long, but it can't do overload resolution properly with the
int
version
so if I put that in my utils then all my previous solutions which did import utils.* stop compilling 😛
p
Use sumBy { it.toLong() }
n
and there's no way around having to either stick
L
places, or explicitly specify
Long
that doesn't work
sumBy in the standard library is only for Int, exactly for this issue (can't do overload resolution properly)
p
Then it was called sumOf
n
ah you're right
thanks!
e
note that if you try to enable checked math on stdlib you're gonna have a bad time, plenty of code makes use of wrapping overflow
n
yeah, there are workarounds, and we have had this entire discussion before at the last advent of code 🙂 It's just a real shame to default to 32 bit integers when the "penalty" for using 64 bit integers on native is non-existent on most platforms.
though I understand there are valid JVM reasons for it, it's the practical choice, blah blah etc
p
I just wished I could set some compiler flag to make that the default
I don't think I ever had a case in my career where an overflow wasn't a bug
Same with divison by zero, I always want that one to throw
n
division by zero does throw, right?
p
Only for ints
1.0 / 0.0 is infinity
n
that's pretty ew
you're sure it's not NaN at least?
p
Yep
That sharing is broken
n
It's like that in C++ too, I forgot
I guess the justification is that 0 in floating point is "signed"
maybe there is some good practical reason beyond that, but in many/most cases the "sign" of 0 is arbitrary. I can't see a good argument why IEEE preferred
1.0 / (1.0-1.0)
to be Inf, rather than NaN, but such is life I guess (and this is not specific to Kotlin of course)
e
at least 0.0 / 0.0 is NaN
although NaN is a whole another ballpark of fun issues…
oh right though. you can't assume integer divide by 0 always throws on all targets: it doesn't on Kotlin/JS because that's how the underlying platform works