Top level functions in multiplatform projects


#1

After looking over the available documentation it seems like the best practice (compiler mandate?) for multiplatform projects is to mirror the package structure of the common module in the platform specific modules. But it doesn’t seem to be possible to have top level functions in files of the same name with same package structure in the common module and jvm module.

Here is a practical use case, kotlin has ulp extension property for Doubles on all platforms but for floats it only has ulp extension property on jvm platform. So if I want to make a floating point equality approximation function that works with Double in common module and one that works with Float in jvm module I have to put these functions in files with different names. So I could have com.example.project.Numbers in common with this:

fun Double.feq(comparate: Double, maxUlps: Int): Boolean {
    ...
}

And com.example.project.Numbers in jvm with this:

fun Float.feq(comparate: Double, maxUlps: Int): Boolean {
    ...
}

Each of these files has other utility functions as well. Right now I have to change the name of the Numbers file in jvm module to something like NumbersJvm

This is just an example but when I was thinking about moving a codebase over the multiplatform I thought of several scenarios where I would want to have a structure like this. Is there a solution to this or is it just mandatory to not share file names between common and platform modules when the file contains top level functions or properties?


#2

Beyond giving them a different name (for jvm-ish targets) with @file:JvmName(...) there isn’t a solution at this point. Multiplatform is still not quite at a finished level. You may notice that intellij also doesn’t handle type aliasses well.


#3

I didn’t know about that annotation so thank you, although it is still not quite ideal. It looks like @file:JvmMultifileClass listed on the same page is made for a similar scenario and would be the best solution but it doesn’t seem to work in common package. Perhaps @file:JvmMultifileClass should be the default behavior for a file with the same name in the common module and jvm module.