https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
h

hallvard

11/03/2019, 9:41 PM
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

kpgalligan

11/03/2019, 10:06 PM
Code? Not only does this work, it’s one way to map sdk’s with typealiases. Giving a talk about it.
s

Sam

11/03/2019, 10:38 PM
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

hallvard

11/03/2019, 10:46 PM
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

kpgalligan

11/03/2019, 10:52 PM
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

hallvard

11/03/2019, 10:57 PM
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

Dico

11/05/2019, 12:16 PM
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
3 Views