Optional
type for KotlinWe don't think that Kotlin itself needs
Optional
because it has strongnull
-safe type system that effectively eliminates need in such a wrapper. However there are Java APIs and libraries like RxJava 2 and RxJava 3 which don't acceptnull
values and language-level nullability cannot help with that.We also think that in many cases you can use
sealed class
es to express absent values, however in simple cases like passingString?
through RxJava streamOptional
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:
toOptional()
and toNullable()
.
toInt()
and toBoolean()
.Some
and None
are declared as top level types.
Optional.Some
or Optional.None
.map()
, getOrElse()
, filter()
, etc.
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()
None
(like java.util.Optional.getOrElse()
)val f = optional.toNullable() ?: "fallback"
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
Use the static Optional.toOptional()
to wrap an instance of T
into Optional<T>
.
val values = Observable.just(Some("a"), None, Some("b"))
// Filter Some values.
values
.filterSome()
.test()
.assertValues("a", "b")
// Filter None values.
values
.filterNone()
.test()
.assertValues(Unit) // filterNone() maps None to Unit.
val values = Flux.just(Some("a"), None, Some("b"))
// Filter Some values.
values.filterSome()
// Filter None values.
values.filterNone()
Koptional is available on jcenter. All the releases and changelogs can be found on Releases Page.
Optional
implementation "com.gojuno.koptional:koptional:$koptional_version"
implementation "com.gojuno.koptional:koptional-rxjava2-extensions:$koptional_version"
implementation "com.gojuno.koptional:koptional-rxjava3-extensions:$koptional_version"
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
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.