Ignore @JvmDefault when compiling to JavaScript?

#1

Hi, trying to compile to JavaScript some code that uses the @JvmDefault annotation, the kotlinc-js compiler says:

error: unresolved reference: JvmDefault

Is there a way to ignore these annotations when compiling to JavaScript? (Except preprocessing the source files during build and removing the annotations.)

John

#2

You are probably using an old version of Kotlin. The latest (since 1.3.20 if I remember correctly) version actually properly ignores those.

#3

Strange, must be something different:

$ cat src/main/kotlin/Foo.kt 
interface Foo {

    @JvmDefault
    fun getFoo() = true

}

$ kotlinc-js -version
info: kotlinc-js 1.3.21 (JRE 1.8.0_192-b12)

$ kotlinc-js  -output out/Foo.js -module-kind umd src/main/kotlin/Foo.kt 
src/main/kotlin/Foo.kt:3:6: error: unresolved reference: JvmDefault
    @JvmDefault
     ^
#4

You will have to import the class. It is not in the default package names. Try adding “import kotlin.jvm.JvmDefault” to the file.

#5

Thanks for getting back. I had tried that before:

$ kotlinc-js  -output out/Foo.js -module-kind umd src/main/kotlin/Foo.kt
src/main/kotlin/Foo.kt:1:19: error: unresolved reference: JvmDefault
import kotlin.jvm.JvmDefault
                  ^
src/main/kotlin/Foo.kt:5:6: error: unresolved reference: JvmDefault
    @JvmDefault
     ^
#6

I think it is because your code is not a cross-platform module. The annotation does not exist in the Javascript version, but common modules are allowed to have such annotations and then ignore them later (an unresolved expectation). In other words, to make this work you should probably bite the bullet and actually use a multiplatform project (probably with gradle), or at least include the common version of the standard library.