When the Kotlin documentation mentions the JVM bei...
# compiler
c
When the Kotlin documentation mentions the JVM being ‘open world’ and JS/Native being a ‘closed world compilation’, does that mean that it's not possible to declare an interface in Kotlin and then implement it in JS/C/...? Or did I completely misunderstand what it means?
e
it means that JVM has the expectation that classes can be added at runtime and JS/native assume that all code that exists at runtime was present during compile
t
I think you understood partially correctly. You can still build and publish javascript libraries and "implement"(js doesn't have interfaces) kotlin interfaces in js/ts modules (https://kotlinlang.org/docs/js-to-kotlin-interop.html). I think Kotlin even automatically creates TS declarations. C also does not have interfaces, but it's even more complicated there and I have no experience with it. I don't think you can share kotlin module as "pure" C library. You can read more on https://kotlinlang.org/docs/native-c-interop.html and maybe someone with more interop experience than me can guide you further.
For js npm publishing (unofficial) - https://github.com/mpetuska/npm-publish
e
open/closed-world compilation doesn't really have to do with external code
2
t
@ephemient Agree, but OP expected consequences of closed-world compilation to affect constraint interop more, than it actually does.
@ephemient Does "closed-world compilation" implies more than that effective binaries produced for target platform will have embedded kotlin runtime?
e
it does permit the compiler to perform more optimizations such as inlining and dead code removal than open-world compilation, if that's what you mean
t
@ephemient Ah, right. But I guess it does not affect libraries, before they I linked. Am I correct?
e
yes, not until the final link
t
I'm still curious when inlineing
inline fun
's happen. Especially on JVM, where it can be done either statically during compilation(for specific version of dependency) or with invoke dynamic.
e
like const val, they're inlined (statically) when that module is built. changing inline fun and const val is an API break
t
@ephemient Thanks for clarification : )
c
If in both cases it's possible to extend classes/etc from other non-Kotlin modules, I don't really understand what optimizations can be done
I understood something like “if we know for sure at compile time that this interface has a single implementation, we can skip all the dynamic override calls and directly call the implementor's functions” which is a thing the JVM does with JIT, but if other languages are allowed to add implementations I don't think it's possible
e
right, as far as I'm aware, Kotlin classes can't be added after the JS or native binary is built. but you can define an external interface and have that implemented by whatever at runtime