Alexander
10/05/2021, 8:38 AMDouble
. Here is an example:
val n = 1.1 as Any
if (n is Number) {
println(n.toLong())
println(n.toLong() == 1L)
}
if (n is Int) {
println(n.toLong())
println(n.toLong() == 1L)
}
Both conditions are resolved to true
(I know that Javascript hasn't integer numbers so its ok). But what is more interesting it prints:
1
true
1
false
When I use kotest
it fails with expected:<1L> but was:<1.1L>
.
1.1L
looks really strange for me.Big Chungus
10/05/2021, 8:54 AMBig Chungus
10/05/2021, 8:56 AMephemient
10/05/2021, 8:58 AMBig Chungus
10/05/2021, 9:00 AMephemient
10/05/2021, 9:02 AMNumber.toLong()
actually performs conversion, while Int.toLong()
can assume that it's already truncatedephemient
10/05/2021, 9:03 AMAlexander
10/05/2021, 9:22 AMMap<String, Any?>
and I need to process somehow values from it.Alexander
10/05/2021, 9:57 AMprivate val isJs = (0.1 as Number) is Int
if (isJs && v is Number) {
return v.toLong()
}
Big Chungus
10/05/2021, 9:59 AM// commonMain
expect val isJS:Boolean
// jsMain
actual val isJS = true
// jvmMain
actual val isJS = false
Alexander
10/05/2021, 10:09 AMdetekt
and expect-actual
but then refactored detekt
tasks.
Recently have tested expect-actual
and it works.Tomasz Krakowiak
10/05/2021, 12:30 PMBig Chungus
10/05/2021, 12:45 PMTomasz Krakowiak
10/05/2021, 12:55 PMNumber
type is a double-precision 64-bit binary format IEEE 754 value, like double
in Java or C#."
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberBig Chungus
10/05/2021, 12:57 PMBig Chungus
10/05/2021, 12:57 PMTomasz Krakowiak
10/05/2021, 12:57 PMBig Chungus
10/05/2021, 12:58 PMBig Chungus
10/05/2021, 12:58 PMTomasz Krakowiak
10/05/2021, 1:00 PMBig Chungus
10/05/2021, 1:00 PMTomasz Krakowiak
10/05/2021, 1:04 PMRepresents a 32-bit signed integer. On the JVM, non-nullable values of this type are represented as values of the primitive type int.
Int kotlindoc.
And we can see strange side effects, as OP posted, caused by violation of Kotlin semantics.Big Chungus
10/05/2021, 1:06 PMSvyatoslav Kuzmich [JB]
10/05/2021, 1:36 PM|
) tricks or Math.imul
(JS engines are good at compiling these to native 32-bit integer instructions).
The problem that breaks contract seems to be with is
type checks. It might be worth checking for 32-bitness there.Tomasz Krakowiak
10/05/2021, 1:39 PMSvyatoslav Kuzmich [JB]
10/05/2021, 1:43 PMSvyatoslav Kuzmich [JB]
10/05/2021, 1:45 PMTomasz Krakowiak
10/05/2021, 1:48 PMephemient
10/05/2021, 4:50 PMTomasz Krakowiak
10/05/2021, 4:54 PMephemient
10/05/2021, 5:09 PMTomasz Krakowiak
10/05/2021, 5:11 PMephemient
10/05/2021, 5:13 PMTomasz Krakowiak
10/05/2021, 5:14 PMTomasz Krakowiak
10/05/2021, 5:16 PMephemient
10/05/2021, 5:33 PM|0
that current JS implementations use to recognize limited-precision on integer operations, for bigint