Multiplatform and JVM Statics (java.util.Optional)


#1

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


#2

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.


#3

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


#4

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.


#5

Do you mean

@JvmStatic

?


#6

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