I came across a java method that takes a java.util...
# announcements
s
I came across a java method that takes a java.util.Optional. Does the kotlin lib provide a simple method to convert a nullable to an optional?
k
Java already has
Optional.ofNullable(x)
m
As always, if you’re not happy with the available options, define your own. For example:
Copy code
fun <T: Any> T?.asOptional(): Optional<T> = Optional.ofNullable(this)
or:
Copy code
val <T: Any> T?.optional: Optional<T>
    get() = Optional.ofNullable(this)
j
to quote @raulraja: nullable types are isomorphic with Option types but they have performance on thier side