koptional
koptional copied to clipboard
Minimalistic Optional type for Kotlin that tries to fit its null-safe type system as smooth as possible.
Koptional — Minimalistic Optional type for Kotlin
We don't think that Kotlin itself needs
Optionalbecause 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 acceptnullvalues 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 passingString?through RxJava streamOptionalis 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()andtoNullable().- Mimics Kotlin functions like
toInt()andtoBoolean().
- Mimics Kotlin functions like
SomeandNoneare declared as top level types.- No need to write
Optional.SomeorOptional.None.
- No need to write
- No functions like
map(),getOrElse(),filter(), etc.- Use
toNullable()and Kotlinstdlibfunctions likelet(),takeIf()instead.
- Use
Usage
Create
val some = Some(value)
val none = None // It's an object!
Convert
// 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!")
}
Destructuring
// 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 Extensions
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.
Reactor Extensions
val values = Flux.just(Some("a"), None, Some("b"))
// Filter Some values.
values.filterSome()
// Filter None values.
values.filterNone()
Download
Koptional is available on jcenter. All the releases and changelogs can be found on Releases Page.
Optional
implementation "com.gojuno.koptional:koptional:$koptional_version"
RxJava 2 Extensions
implementation "com.gojuno.koptional:koptional-rxjava2-extensions:$koptional_version"
RxJava 3 Extensions
implementation "com.gojuno.koptional:koptional-rxjava3-extensions:$koptional_version"
Reactor Extensions
implementation "com.gojuno.koptional:koptional-reactor-extensions:$koptional_version"
License
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.