Minimalistic Optional type for Kotlin that tries to fit its null-safe type system as smooth as possible.
Clone or download
radekkozak and artem-zinnatullin Hide component1() destructuring from Java consumers (#30)
I propose simple change that i think would be beneficial from the perspective of java consumers, namely annotating `component1()` function with kotlin's `@JvmSynthetic`

As a consequence method will become invisible from Java. Long story short i think it's rather unneccesary to pollute java with a method that quite frankly doesn't say anything for them as the concept of kotlin's destructuring is simply not there. For getting actual values there is `toNullable` already so i don't see a point

Right now it looks like this:

Latest commit a8c7191 Aug 1, 2018

Koptional — Minimalistic Optional type for Kotlin

We don't think that Kotlin itself needs Optional because it has strong null-safe type system that effectively eliminates need in such a wrapper. However there are Java APIs and libraries like RxJava 2 which don't accept null values and language-level nullability cannot help with that.

We also think that in many cases you can use sealed classes to express absent values, however in simple cases like passing String? through RxJava stream Optional is a more convenient solution.

The goal of this implementation is to be convenient to use and fit Kotlin's null-safe type system, which resulted in:

  • Only two functions: toOptional() and toNullable().
    • Mimics Kotlin functions like toInt() and toBoolean().
  • Some and None are declared as top level types.
    • No need to write Optional.Some or Optional.None.
  • No functions like map(), getOrElse(), filter(), etc.
    • Use toNullable() and Kotlin stdlib functions like let(), takeIf() instead.



val some = Some(value)
val none = None // It's an object!


// T? → Optional<T>
// If value is null — you'll get None, otherwise you'll get Some(value).
val o = value.toOptional()

// Optional<T> → T?
// If optional is None — you'll get null, otherwise you'll get non-null T value.
val t = optional.toNullable()

Leverage Kotlin Features

Fallback from None (like java.util.Optional.getOrElse())

val f = optional.toNullable() ?: "fallback"

Smart Cast

when (optional) {
    is Some -> println(optional.value)
    is None -> println("Nope!")


// If Optional is None — you'll get null, otherwise you'll get non-null T value.
val (value) = optional

Java Interop

Use the static Optional.toOptional() to wrap an instance of T into Optional<T>.

RxJava 2 Extensions

val values = Observable.just(Some("a"), None, Some("b"))

// Filter Some values.
    .assertValues("a", "b")

// Filter None values.
    .assertValues(Unit) // filterNone() maps None to Unit.

Reactor Extensions

val values = Flux.just(Some("a"), None, Some("b"))

// Filter Some values.

// Filter None values.


Koptional is available on jcenter. All the releases and changelogs can be found on Releases Page.


implementation "com.gojuno.koptional:koptional:$koptional_version"

RxJava 2 Extensions

implementation "com.gojuno.koptional:koptional-rxjava2-extensions:$koptional_version"

Reactor Extensions

implementation "com.gojuno.koptional:koptional-reactor-extensions:$koptional_version"


Copyright 2017 Juno, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.