:warning: *The Kotlin Multiplatform division-by-ze...
# library-development
l
⚠️ The Kotlin Multiplatform division-by-zero trap:
12 / 0
behaves differently on every platform
On Kotlin/JS,
12 / 0
silently returns
0
. On JVM and Native it throws
ArithmeticException
. That guard you wrote and tested on JVM? It is silently bypassed when the same code runs on JS. We wrote a short post about what causes this and how the
Integer
type in Kotools Types 5.1.1 fixes it consistently across all three targets — including null-safe
divOrNull
and
remOrNull
variants for those who prefer not to catch exceptions. Read it here 👇
d
I recommend filing a bug with Jetbrains. This library uses a String to represent integers in some platforms. That's a red flag and complete non-starter. Swapping this in would probably make any integer calculations over 100 times slower and also introduces a ton of garbage collection overhead.
👍 1
a
Thank you for sharing the pain point 🙏 We've planned the fix of this behavior and want to ship it in the following release (2.4.20). You can watch to the updates here: https://youtrack.jetbrains.com/issue/KT-17719
👍 1
l
@Dan Rusu Starting from Kotools Types 5.2 (coming this month),
Integer
will use numeric platform-specific types instead of
String
. This would definitely improve performance of integers' arithmetics. See its changelog.