Skip to content
Alex Zuzin edited this page Apr 13, 2017 · 2 revisions

Kotlin has first-class nullables, so the Option constructor can be null-aware:

val nada: None = option(null)
val bonus: Some<Int> = option(1)

An ad-hoc Option can also be created in the usual way:

val none = none()
val some = some("well, there's that")

Options can be predicated on a boolean value using a value or a factory method:

val nada: None = false.option("Fabulous expression of beauty")
val bonus: Some<String> = true.option("Bathroom poetry")
    
val nadaFun: None = false.option { -> "Fabulous expression of beauty"}
val bonusFun: Some<String> = true.option { -> "Bathroom poetry"}

A Kotlin Iterable of Options can be sequenced as follows:

val optionOfSeq: Option<Seq<Int>> = listOf(option(1), option(null)).sequence()
Clone this wiki locally