For example I want to define an interface:
In commonMain:
expect interface Test {
fun test(a: Other1): Other2
}
In jvmMain:
actual interface Test {
@JvmDefault
fun test(a: Other1): Other2 {
...
}
}
But the compiler will say
Actual class 'Test' has no corresponding members for expected class members:
public abstract expect fun test(a: Other1): Other2
The following declaration is incompatible because modality is different:
public open actual fun test(a: Other1): Other2
I canât find any way to allow default implementation in the actual interface.
The solution is to add the @JvmDefault annotation to the expect clause. The standard library has been updated to ignore this annotation on non-jvm platforms.
No, the JvmDefault annotation is only supported on the JVM platform.
You can see the document only show a green point (meaning only supported on JVM) for the JvmDefault annotation.
I have also try to write an expect annotation and on JVM platform make it a typealias of JvmDefault, and use this annotation for the default implementation of an interface. But this does not help at all. The compiler still say the âmodality is differentâ.
In my situation, I have to ensure that test is a default implementation in the interface Test.
With your solution, if someone want to use the JAR you build in Java, he / she has to import a class like TestImpl and use TestImpl.test instead of just implementing the interface Test.