diff --git a/README.md b/README.md index 8c5543d..a11239c 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,19 @@ when (optional) { } ``` +#### Destructure `Optional` + +Koptional supports [destructuring](https://kotlinlang.org/docs/reference/multi-declarations.html). + +Destructuring has same effect as calling `toNullable()`. + +```kotlin +val o: Optional = something.toOptional() + +// If Optional is None — you'll get null, otherwise you'll get not null T value. +val (value) = o +``` + #### Filter only `Some` values emitted by RxJava 2 or Project Reactor ```kotlin @@ -80,12 +93,6 @@ val noneSignals: Observable = Observable Use the static `Optional.toOptional()` method (declared as a companion object method) to wrap an instance of `T` into `Optional`. -#### Destructure `Some` into T - -```kotlin -val (value) = Some("a") -``` - ### Download Koptional is [available on jcenter](https://jcenter.bintray.com/com/gojuno/koptional). @@ -109,7 +116,7 @@ All the releases and changelogs can be found on [Releases Page](https://github.c Dependencies: you only need `docker` and `bash` installed on your machine. ```console -bash ci/build.sh +ci/build.sh ``` ## License diff --git a/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt b/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt index b32ead2..49855ab 100644 --- a/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt +++ b/koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt @@ -101,40 +101,32 @@ class OptionalSpec : Spek({ } } - describe("component1") { + describe("destructuring") { - context("Optional.component1") { + context("destructure Optional") { - val (result) = 42.toOptional() + val (result: Int?) = 42.toOptional() - it("destructures it to Int value") { + it("destructures to its value") { assertThat(result).isEqualTo(42) } } - context("None.component1") { + context("destructure None") { - val (result) = (null as Int?).toOptional() + val (result: Int?) = (null as Int?).toOptional() - it("destructures it to null") { + it("destructures to null") { assertThat(result).isNull() } } - context("Lambda destructuring") { - it("destructures to Int value") { - listOf(Some(42)).forEach { (value) -> - assertThat(value).isEqualTo(42) - } - } - } - context("destructure Some") { - val some = Some("string") + val (result: String) = Some("string") - it("destructures to non-null type") { - val (value: String) = some + it("destructures to its value") { + assertThat(result).isEqualTo("string") } } }