Is there a known problem with defining extension f...
# multiplatform
h
Is there a known problem with defining extension functions within files that have expect/actual stuff? I get a
Duplicate JVM class name
error from gradle when trying to build (and if I move my extension function to a file that has no
expect
functions, everything is hunky dory).
k
Code? Not only does this work, it’s one way to map sdk’s with typealiases. Giving a talk about it.
s
I've seen that problem but can't recall how I fixed it. I think the files names were the same but didn't have the same case.
h
Three files, all named ExtensionFunctions.kt, one in common, one in jvm and one in js. The two actuals are identical, they contain this:
Copy code
package com.sannsyn.example

actual fun expectMeForDinner(): String {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
The file in common contains this:
Copy code
package com.sannsyn.example

import kotlin.js.JsName

open class John {

    private val backing: Map<String, Any?>

    @JsName("JohnFromMap")
    constructor(map: Map<String, Any?>) {
        require(map.isNotEmpty()) { "Cannot construct a John object from empty map." }
        backing = map.toMap() // i.e. create a copy
    }

}

expect fun expectMeForDinner(): String


// To get a John object out of a Map<String, *>:
public fun Map<String, Any?>.toJohn() = John(this)
Gradle version 5.6.3
k
I generally name jvm files something different.
ExtensionFunctionsJVM.kt
. Something like that. You can run into naming issues on the jvm as the compiler makes a class for the file when there are top level things, and an extension is that.
h
I solved this by moving my new extension function elsewhere. Seems the problem was related to an extra not-expect/actual function living alongside of expect/actual functions in the same file, outside of a class declaration. No big deal.
d
Yeah, it's when the file with expect declarations has non-expect declarations and there's a file with the same name in another sourceset
Been an issue since the introduction of multiplatform