https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
n

natpryce

03/24/2023, 9:19 AM
The mapping of the W3C DOM IDL to Kotlin is different on JVM and JS platforms. On JVM, the DOM interfaces are mapped to Kotlin interfaces. On the JS platform, the DOM interfaces are mapped to Kotlin classes. Is this a defect?
b

Big Chungus

03/24/2023, 1:21 PM
It's not. On jdk they're defined as interfaces whereas in the browser they are classes (since js doesn't have interfaces)
n

natpryce

03/24/2023, 8:43 PM
JS doesn’t really have classes in the Kotlin sense either.
b

Big Chungus

03/24/2023, 8:43 PM
True, it's all functions in a trench coat
n

natpryce

03/24/2023, 8:44 PM
I should be able to write a Kotlin class that implements a W3C DOM interface and other interfaces, but that’s not possible in the existing mapping. Which I consider to be a bug in the mapping of IDL to Kotlin on the JS platform.
(It also makes writing multiplatform code that uses the W3C DOM impossible)
b

Big Chungus

03/24/2023, 8:44 PM
You still can, just don't use dom classes from stdlib and declare them as external interfaces yourself
n

natpryce

03/24/2023, 8:47 PM
Not ideal… that’s a lot of busywork to re-declare types that are already declared. Is it possible to declare external interfaces that have the same fully qualified name as a type in the stdlib?
b

Big Chungus

03/24/2023, 8:49 PM
Kinda, but it's hacky. You basically suppress expect with no actual errors in common code and actual with no expect in platform code. But doing so removes all compile-time guarantees
n

natpryce

03/24/2023, 8:50 PM
And requires re-declaring the entire DOM API by hand.
And requires re-declaring the entire DOM API by hand. No, you can still typealias
n

natpryce

03/24/2023, 8:52 PM
But wouldn’t I need to declare the interface methods in the
expect
declarations?
b

Big Chungus

03/24/2023, 8:52 PM
Correct
n

natpryce

03/24/2023, 8:53 PM
That’s what I mean by redeclaring the DOM API
b

Big Chungus

03/24/2023, 8:53 PM
But you'd need to do that for any kmp code that relies on platform APIs
n

natpryce

03/24/2023, 8:54 PM
Except that the APIs should be exactly the same on JS and JVM – generated from the IDL published by W3C and in the stdlib on both JVM and JS platforms.
The only difference is that Kotlin JS declares them as classes and the JVM declares them as interfaces.
But apart from that keyword, they have the same methods.
b

Big Chungus

03/24/2023, 8:54 PM
Tell that to JDK API folks 😀
And they can't be identical anyways. How do you propose registering event handlers on jvm?
n

natpryce

03/24/2023, 8:55 PM
Interfaces is exactly what they are though. You cannot instantiate them. Even on JS, you have to ask the DOM implementation to create instances.
That’s DOM level 1 v level 2. I think having different DOM levels on each platform is fine. But declaring the mapping of, say, the same DOM level 1 API to be different declarations feels wrong, and makes writing multiplatform code impossible (at least, with the Kotlin multiplatform language feature. It’s possible with old-skool C techniques for writing multiplatform code)
Anyway… gotta go. I’ll have a think…
10 Views