A small proposal, currently on Kotlin Multiplatfor...
# language-proposals
f
A small proposal, currently on Kotlin Multiplatform if someone wants to define inline classes one has to use @JvmInline annotation on jvm sourcesets and not use it on non-jvm sourcesets which creates an unnecessary platform sourceset split since the code could otherwise be platform agnostic. Since it is an annotation to indicate to the JVM backend to use a specific opitimazation other targets (Kotlin/JS and Kotlin/Native) could just ignore it without throwing an error. (Treat it as a no-op on non-jvm backends) Filed on YouTrack: https://youtrack.jetbrains.com/issue/KT-53404/Treat-JvmInline-annotations-as-no-op-on-non-jvm-backends-for-better-Multiplatform-experience Example on playground: https://pl.kotl.in/R4XMcQfCJ
Copy code
// ^ Try different backends
// v And commenting out or not
// @JvmInline
value class NonEmptyString(val value: String) {
    init {
        require(value.isNotEmpty())
    }
}

fun main() {
    println(NonEmptyString("Hello"))
    try {
        println(NonEmptyString(""))
    } catch(e: Throwable) {
        println("Woops!")
    }
}
j
I like the idea of having a common
@Static
annotation which would map to
@JvmStatic
on Kotlin/JVM, while introducing a similar annotation for Kotlin/Native, maybe
@NativeStatic
. I created this YouTrack for this.
f
Though, that would be slightly different no? As in even when JvmStatic gets introduced it should also be no-op on non-jvm backends such as Kotlin/Js and Kotlin/Native to avoid having to split those inline into different sourcesets on multiplatform projects.
Ah right, so
@Static
would work on all backends (even non implemented ones in that hypothetical such as Kotlin/JS)
👍 1
t
I think it would be great to have conditional compiling in Kotlin, and you can just
#if JVM
to define anything for just JVM code, and there will be no need to define a common
@Static
👍 1
j
Yeah, similar idea where a common annotation works on all platforms where it has an optimization implementation and where it doesn't, it'd fall back to a no-op.
f
NVM, it is already a no-op, I was missing an import in non-jvm backends.