Okay, one more. Is there a way to reference a Java...
# javascript
m
Okay, one more. Is there a way to reference a JavaScript
Number
type in Kotlin?
Number
is
kotlin.Number
, which is not what I want. Is there a
javascript.Number
or something?
t
Why do you need it?
m
Doing some bitwise things and low-level operations for JSExported functions and wanted to know exactly what I was dealing with when someone passes me a value via a parameter.
Kotlin numeric types, except for
kotlin.Long
are mapped to JavaScript
Number
.
The above is from the docs. You can access other JS types from Kotlin, so I figured there was a way to get at JS
Number
directly.
t
Double
?
m
JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers.
Feels weird to do bitwise stuff on it, especially since,
JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers.
e
what that means is that JS's
x & y
behaves like
(x.toInt() and y.toInt()).toDouble()
in Kotlin
a bit operation in JS still results in a JS Number albeit with a smaller range that modern JS engines do try to do clever things with the representation for speed
m
Yeah I figured. So just use
Double
. That's kind of the answer I was expecting, but figured I would ask. IDK why JS can't have nice numeric types like every other language.
Thank you for the answer.
t
What about
BigInt
?
Is it fine for your cases?
m
Reading about it...
e
it's not the only language like that: Lua too
m
Are there bounds on
BigInt
? Or can it hold arbitrarily large numbers?
I can't find anything saying it's bounded so that may be fine.
Typed
BigInt
and autocomplete didn't help me... How do I reference a BigInt from Kotlin?
to use in Kotlin, just declare externals like for anything else
t
m
Oh cool.
So
BigInt
is available in all (modern) JS runtimes right?
It feels weird to grab the
kotlin-wrappers
for something that's in the stdlib. Figured that would all be mapped already in the kotlin-stdlib.
This help is great by the way. Thank you all for sharing your knowledge!
Yeah okay I've got it working now I think. Only question let remaining is what the heck are these version numbers
1.0.1-pre.156-kotlin-1.5.0
Very strange looking
t
t feels weird to grab the
kotlin-wrappers
for something that’s in the stdlib. Figured that would all be mapped already in the kotlin-stdlib.
In awesome future Kotlin/JS it will be as you expect (I suppose) with
N
JS platforms support. Right now we add required missed types in wrappers for people who need it right now (like me, like you). Also important, that we release wrappers very often. cc @Sergei Grishchenko
m
Okay that's very good to know. Thanks again 🙂
t
Also we have full
console
API support and more usefull timing helpers (
setTimeout
, …)
what the heck are these version numbers
1.0.1-pre.156-kotlin-1.5.0
${npm-lib-version}-${build-version}-${kotlin version}
And this question is open 🙂
BOM can hide this question 🙂
m
Yes I saw the BOM. Seems weird to me that there's no 1.6.x kotlin version. I'm guessing it's compatible but I'll find out.
t
0.0.1-pre.331-kotlin-1.6.20
- latest version
Released this week 🙂
m
Oh. I did not see that in maven central
t
m
That is because I was looking at the wrong artifact in maven central 🤦‍♂️
t
You can click shields in main README
m
That is good to know
Oh... It doesn't look like I can go from
BigInt
to any other type...
Turns out it's as easy as passing it to the
Number
constructor:
```const myBigInt = BigInt(10); //
10n
also works
const myNumber = Number(myBigInt);```
https://stackoverflow.com/a/53970656
My oh my we have gone full circle. How do I get the
Number
constructor?
I have found
kotlinx.js.JsNumber
, however... None of the constructors take
BigInt
.
Copy code
package kotlinx.js

@JsName("Number")
external class JsNumber {
    internal constructor(value: Int)
    internal constructor(value: Double)
    internal constructor(value: String)
This is what the declaration looks like from
sources.jar
t
You don’t need constructor
Copy code
@JsName("Number")
external fun Double(
    value: BigInt, 
): Double
m
Is that in the extensions wrapper?
t
Not right now 🙂 You can add it locally for start
m
Will do, excellent.
Wait but I don't want to convert it to a double. I need access to the additional precision in Kotlin.
My fault of course by not thinking it through before asking 🙂
Getting it as a string would probably be ideal. I guess I'll try out
toString
. The answer was probably there the whole time
t
🙂
m
It works very well. Thank you.
I learned a lot today 🙂
WrappedLong.kt
WrappedLong.kt
@turansky this is the result of what I was working on.
jvmMain
and
nativeMain
are trivial and left as exercises for the reader 😉
Thank you again for all the help
t
Do you create library or final application?
m
This is a library. Why do you ask?
e
if your operations fit in a Long, Kotlin/JS's Long emulation will be significantly faster than JS BigInt (see earlier thread for a link to benchmarks)
m
Yeah I know, but I need to be able to use these functions directly from JS.
Without precision loss.
e
if you're going to stringify them, JS won't see kotlin.Long
m
I'm talking about JS needing to pass a large integer into Kotlin, do things with it, then return it.
All without precision loss.
JS can't use Kotlin's emulated longs, sadly.
e
are you doing arithmetic on the numbers in JS? if not, just use strings for everything on the JS side. and even if you are, if you're multiplatform you will probably have common Kotlin functions working on Long, which means you need to convert to/from the JS representation anyway, and since that has to go through String you might as well use that as the JS interface and leave other side of the conversion to JS
m
Just read the benchmarks you sent. That's not my use case. The math inside Kotlin land will only be Long math, and will therefore be fast. The only point where BitInt will be used in our library is in this conversion function, to give JS the option to both provide and receive full precision.
For a majority of our users the WrappedLong.toDouble and wrappedLongFromDouble functions I've written will be plenty. But because I work on medical software, I need to be able to provide full precision as an option.
e
to make my position clearer. since the Long <-> BigInt conversion has to go through String anyway, use String as the JS interface. JS users that need a BigInt can perform the conversion themselves. JS users that just need opaque data can skip the conversion and leave it as String.
m
Oh that is what you are saying. That I don't need the ref to big int at all.
That's a fine point. I'll think about that.
In retrospect that is pretty obvious now that you say it. But the way I originally approached the problem was "how do I represent large integers in Javascript" which led me down the BigInt path.
e
yep. there are certainly other uses for BigInt, but if it's just to carry a Long, I don't think it's a good use.
t
Yeah okay I’ve got it working now I think. Only question let remaining is what the heck are these version numbers
1.0.1-pre.156-kotlin-1.5.0
Now version is shorter
❤️ 1