Giter Club home page Giter Club logo

koptional's Introduction

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"
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 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.

koptional's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

koptional's Issues

Introduce infix `or` function

It will be really handy to have infix or function in Optional for fallback to default value in case of None like:

val some = Some(1)
val someOrDefault = some or 2

Provide some functional operations

Implementation functional operations for Option like scala, java 8

sealed class Optional<out T : Any> : Serializable {
    val isPresent: Boolean get() = this != None
}

inline fun <T : Any> Iterable<Optional<T>>.foreach(action: (T) -> Unit): Unit =
    this.filter { it.isPresent }.map { (it as Some<T>).value }.forEach(action)

inline fun <T : Any, R> Iterable<Optional<T>>.flatMap(mapper: (T) -> R): List<R> =
    this.filter { it.isPresent }.map { (it as Some<T>).value }.map(mapper)

Design: auto-folding Some(None) → None

@weefbellington from our team at Lyft has been working on serializing/deserializing Koptional values in JSON and we've stuck for some time discussing following use case:

Optional<Optional<T>> (and other levels of direct nesting).

This use case is weird on its own, however Koptional does nothing to prevent it (neither does Arrow btw), so it's a valid state for Koptional 1.x.

Right now we've decided to serialize/deserialize without enhancing JSON with additional metadata, which results in following convention for JSON:

Some(None) → None

However, that creates difference between how we represent Some(None) in-memory vs JSON.


With all that in mind, I'd like to raise a discussion about this and propose following changeset for Koptional 2.x:

  • Hide Some constructor
  • Expose a function named Some(): Optional<T> that would return None for Some(None)

cc @ming13 @dmitry-novikov @nostra13 @AlecStrong @Egorand

component1 for Optional?

Often times in code i do something like:

optionalStream()
  .subscribe { fooOptional ->
    val foo = fooOptional.toNullable()
    if (foo != null) ...
    else ...
  }

with operator fun component1() = toNullable() implemented on Optional I could instead do:

optionalStream()
  .subscribe { (foo) ->
    if (foo != null) ...
    else ...
  }

tiny optimization. Happy to contribute this if it makes sense but since its api changing I figured I'd issue first in case it doesnt make sense

Observable<out ...> support for extensions

Example:

val stream = if (true) {
    Observable.just(Some(IOException()), None)
} else {
    Observable.just(Some(RuntimeException()), None)
}

stream.filterSome() // Compile error

stream is of type Observable<out Optional<Exception>> and filterSome() (and other RxJava 2 extensions) can't be applied to such streams. Need to fix it.

Remove Any constraint on type parameter?

Currently, the type parameter of Optional is constrained to not be nullable; however, if it weren't, it would allow an important distinction between no value and not present (e.g.: deserializing data).

Would this be possible (or make sense), and would it fit the design goals of this library?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.