Multiplatform and JVM Statics (java.util.Optional)

How does one go about creating a common version of optional and its actual implementation ?

I have tried this :

expect class Optional<T> {
    open fun get() : T
    open fun isPresent(): Boolean
    companion object {
        inline fun <reified T> empty() : Optional<T>
    }
}

actual typealias Optional<V> = java.util.Optional<V>

but I get the following error:
Actual class ‘Optional’ has no corresponding members for expected class members:
public expect companion object

java.util.Optional does not have a companion object, so the best you can do for now is to define top-level expect fun <T> emptyOptional() and provide an actual implementation for it.

As alternative funKTionale’s Options should work in JVM and JS.

Thanks for the tip, it would be nice if there was a way for providing a mapping of “static methods” via companion objects to JVM static methods.

1 Like

Do you mean

@JvmStatic

?

@Jvmstatic cannot be used in a common library, only in the JVM library