On KMP with interfaces, is binary compatibility pr...
# library-development
b
On KMP with interfaces, is binary compatibility preserved if I add a new member as long as the new member has a body? The answer seems to be yes (at least on the multiplatform targets I tested with), but I wasn't able to find any reassurance while poking through documentation
For example, if a library consumer targets an old library version before the interface member was added, but has the new library version at runtime:
Copy code
// library
interface Foo { // added in v1.0
    fun bar(): String { // added in v1.1
        return "baz"
    }
}

fun consumeFoo(foo: Foo) {
    println(foo.bar()) // added in v1.1
}
Copy code
// library-consumer, compiled against v1.0
class FooImpl : Foo

fun main() {
    consumeFoo(FooImpl())
}
If library-consumer is executed with v1.1, will consumeFoo work fine on all platforms even though FooImpl was compiled without any knowledge of the
bar
method? I tested this with library-consumer compiled using
compileOnly("library:1.0")
+
runtimeOnly("library:1.1")
and it seemed to work fine, though I know the
*Only
dependencies aren't fully supported yet on KMP so I'm not positive that's a good test
a
I don't think it possible to have both 1.1 and 1.0 at runtime, that being said, as long as 1.1 is the one availble at runtime, you won't get any issues. If however you have 1.0 at runtime, then you might experience undefined behaviour. I guess it all boils down to library resolution
b
Having only 1.1 at runtime is what I mean, just that it's been compiled against the older version. So it sounds like new interface bodies are binary compatibility then as long as they have a body
a
Yes, they are. I know on JVM they use the invoke dynamic instruction, so that should work without issues Javascript should cause a big deal as well I am just unsure of Kotlin Native and Kotlin Wasm