Is there a clean way to call functions in a kotlin...
# javascript
r
Is there a clean way to call functions in a kotlinjs generated bundle from the backend (kotlinx.html) in a multiplatform project? I want to avoid creating "raw" blocks and having untyped function calls.
t
Do you have short example?
r
Sure. On the frontend (kotlinjs, compiled into a bundle):
Copy code
fun addHelpers() {
        window.asDynamic().clearModals = ::hideBackdrop
    }

    fun hideBackdrop() {
        val maybeBackdrop = document.body!!.lastChild!!
        if (maybeBackdrop is HTMLElement) {
            if (maybeBackdrop.nodeName.lowercase() == "div" && maybeBackdrop.hasAttribute("modal-backdrop")) {
                maybeBackdrop.remove()
            }
        }
    }
Backend (kotlinx.html):
Copy code
script {
                unsafe {
                    raw("""
                        window.clearModals();
                        """.trimIndent())
                }
            }
(this is a fairly trivial example, but imagine e.g. providing data series and options to a chart - it gets really ugly)
Any thoughts on this @turansky?
t
Looks like you want "special interop", which you can implement in different ways. It looks like possible, but it doesn't look like language responsibility.
r
Hm, don't you think multiplatform should provide glue between different layers? Right now it's not kotlin top to bottom of the app, because you need to "shell out" to javascript, which is untyped
I.e. you own the compiler, the IDE, the frontend language, the backend language - so you have the opportunity to create the holy grail of a unified stack. Right now the gap is still very awkward
t
You can write bridge right now just in Kotlin if you want.
r
What would that look like?
t
expect/actual
In Java it will generate JS code (string)
r
What does that mean @turansky? I.e. how would I use that?
t
Copy code
actual class A {
    fun do()
}
// Java
expected class A {
    fun do() = "<http://window.do|window.do>()"  
}
//JS
expected class A {
    fun do() { your code }
}
fun main() {
    install(A())
}
Works fine for functions with serializable parameters or without parameters.
r
interesting, i will try it