• Stars
    star
    285
  • Rank 145,115 (Top 3 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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 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 and RxJava 3 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.

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.

More Repositories

1

minimock

Powerful mock generation tool for Go programming language
Go
626
star
2

composer

Reactive Android Instrumentation Test Runner. Archived. Marathon is recommended as an alternative (https://github.com/Malinskiy/marathon).
Kotlin
547
star
3

swarmer

Reactive tool to create and start multiple Android Emulators in parallel.
Kotlin
201
star
4

test_tasks

Juno Inc., Test Tasks
42
star
5

engineering

Juno Engineering Blog
33
star
6

jrg

Juno Reverse Geocoder
Gherkin
28
star
7

genval

Generates Validate() methods for structs by tags
Go
28
star
8

lostgis

PLpgSQL
25
star
9

commander

Set of reactive functions for cli tools like Swarmer and Composer.
Kotlin
20
star
10

geo-py

Set of algorithms and structures related to geodesy
Python
17
star
11

osrm-py

Python client for OSRM API
Python
16
star
12

tracegen

Simple tool to generate opentracing decorators
Go
15
star
13

hexgrid-py

Configurable hex grid on abstract surface
Python
14
star
14

make-profiler

Makefile profiling toolset
Python
10
star
15

metricsgen

Tool to generate metrics decorators for interfaces
Go
8
star
16

hexgridgeo-py

GEO wrapper for HexGrid
Python
7
star
17

go-zooz

Zooz API client for Go
Go
7
star
18

generator

Package that simplifies development of go source code generators
Go
6
star
19

junocfg

Template based config generator
Go
6
star
20

morton-py

Morton Pack/Unpack Library
Python
4
star
21

go.morton

Morton Pack/Unpack Library
Go
4
star
22

hexgrid-pgsql

Configurable hexgrid
PLpgSQL
4
star
23

hexgrid-java

HexGrid
Java
4
star
24

goose

Fork of the https://bitbucket.org/liamstask/goose.git without SQLite support
Go
3
star
25

FraktalBand

Swift
3
star
26

TestingPresentableComponents

Swift
3
star
27

FraktalSimplified

Swift
2
star
28

RetryIt

Swift
1
star
29

morton-java

Morton Pack/Unpack Library
Java
1
star
30

frontend-config

JavaScript
1
star
31

morton-js

Morton Pack/Unpack Library
JavaScript
1
star
32

svgen

Scanners and valuers generator for basic types aliases
Go
1
star
33

go-checkout

Checkout.com API client
Go
1
star
34

go.hexgrid

HexGrid
Go
1
star
35

MortonSwift

Swift
1
star