Skip to content

Commit 4acaa4d

Browse files
Destructuring refinements. (#26)
Addresses remaining feedback of PR #20.
1 parent 8b25627 commit 4acaa4d

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ when (optional) {
5757
}
5858
```
5959

60+
#### Destructure `Optional<T>`
61+
62+
Koptional supports [destructuring](https://kotlinlang.org/docs/reference/multi-declarations.html).
63+
64+
Destructuring has same effect as calling `toNullable()`.
65+
66+
```kotlin
67+
val o: Optional<T> = something.toOptional()
68+
69+
// If Optional is None — you'll get null, otherwise you'll get not null T value.
70+
val (value) = o
71+
```
72+
6073
#### Filter only `Some` values emitted by RxJava 2 or Project Reactor
6174

6275
```kotlin
@@ -80,12 +93,6 @@ val noneSignals: Observable<Unit> = Observable
8093
Use the static `Optional.toOptional()` method (declared as a companion object method) to wrap an
8194
instance of `T` into `Optional<T>`.
8295

83-
#### Destructure `Some<T>` into T
84-
85-
```kotlin
86-
val (value) = Some("a")
87-
```
88-
8996
### Download
9097

9198
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
109116
Dependencies: you only need `docker` and `bash` installed on your machine.
110117

111118
```console
112-
bash ci/build.sh
119+
ci/build.sh
113120
```
114121

115122
## License

koptional/src/test/kotlin/com/gojuno/koptional/OptionalSpec.kt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,40 +101,32 @@ class OptionalSpec : Spek({
101101
}
102102
}
103103

104-
describe("component1") {
104+
describe("destructuring") {
105105

106-
context("Optional<Int>.component1") {
106+
context("destructure Optional") {
107107

108-
val (result) = 42.toOptional()
108+
val (result: Int?) = 42.toOptional()
109109

110-
it("destructures it to Int value") {
110+
it("destructures to its value") {
111111
assertThat(result).isEqualTo(42)
112112
}
113113
}
114114

115-
context("None.component1") {
115+
context("destructure None") {
116116

117-
val (result) = (null as Int?).toOptional()
117+
val (result: Int?) = (null as Int?).toOptional()
118118

119-
it("destructures it to null") {
119+
it("destructures to null") {
120120
assertThat(result).isNull()
121121
}
122122
}
123123

124-
context("Lambda destructuring") {
125-
it("destructures to Int value") {
126-
listOf(Some(42)).forEach { (value) ->
127-
assertThat(value).isEqualTo(42)
128-
}
129-
}
130-
}
131-
132124
context("destructure Some") {
133125

134-
val some = Some("string")
126+
val (result: String) = Some("string")
135127

136-
it("destructures to non-null type") {
137-
val (value: String) = some
128+
it("destructures to its value") {
129+
assertThat(result).isEqualTo("string")
138130
}
139131
}
140132
}

0 commit comments

Comments
 (0)