@@ -4,75 +4,66 @@ import kotlin.contracts.InvocationKind
4
4
import kotlin.contracts.contract
5
5
6
6
/* *
7
- * Returns the [value][Ok .value] if this [Result] is [Ok ], otherwise `null`.
7
+ * Returns the [value][Result .value] if this result [ is ok][Result.isOk ], otherwise `null`.
8
8
*
9
9
* - Elm: [Result.toMaybe](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#toMaybe)
10
10
* - Rust: [Result.ok](https://doc.rust-lang.org/std/result/enum.Result.html#method.ok)
11
11
*/
12
12
public fun <V , E > Result <V , E >.get (): V ? {
13
- contract {
14
- returnsNotNull() implies (this @get is Ok <V >)
15
- returns(null ) implies (this @get is Err <E >)
16
- }
17
-
18
- return when (this ) {
19
- is Ok -> value
20
- is Err -> null
13
+ return when {
14
+ isOk -> value
15
+ else -> null
21
16
}
22
17
}
23
18
24
19
/* *
25
- * Returns the [error][Err .error] if this [Result] is [Err ], otherwise `null`.
20
+ * Returns the [error][Result .error] if this result [ is an error][Result.isErr ], otherwise `null`.
26
21
*
27
22
* - Rust: [Result.err](https://doc.rust-lang.org/std/result/enum.Result.html#method.err)
28
23
*/
29
24
public fun <V , E > Result <V , E >.getError (): E ? {
30
- contract {
31
- returns(null ) implies (this @getError is Ok <V >)
32
- returnsNotNull() implies (this @getError is Err <E >)
33
- }
34
-
35
- return when (this ) {
36
- is Ok -> null
37
- is Err -> error
25
+ return when {
26
+ isErr -> error
27
+ else -> null
38
28
}
39
29
}
40
30
41
31
/* *
42
- * Returns the [value][Ok .value] if this [Result] is [Ok ], otherwise [default].
32
+ * Returns the [value][Result .value] if this result [ is ok][Result.isOk ], otherwise [default].
43
33
*
44
34
* - Elm: [Result.withDefault](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#withDefault)
45
35
* - Haskell: [Result.fromLeft](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:fromLeft)
46
36
* - Rust: [Result.unwrap_or](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or)
47
37
*
48
38
* @param default The value to return if [Err].
49
- * @return The [value][Ok .value] if [Ok], otherwise [default].
39
+ * @return The [value][Result .value] if [Ok], otherwise [default].
50
40
*/
51
41
public infix fun <V , E > Result <V , E >.getOr (default : V ): V {
52
- return when ( this ) {
53
- is Ok -> value
54
- is Err -> default
42
+ return when {
43
+ isOk -> value
44
+ else -> default
55
45
}
56
46
}
57
47
58
48
/* *
59
- * Returns the [error][Err.error] if this [Result] is [Err], otherwise [default].
49
+ * Returns the [error][Result.error] if this result [is an error][Result.isErr], otherwise
50
+ * [default].
60
51
*
61
52
* - Haskell: [Result.fromRight](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:fromRight)
62
53
*
63
54
* @param default The error to return if [Ok].
64
- * @return The [error][Err .error] if [Err], otherwise [default].
55
+ * @return The [error][Result .error] if [Err], otherwise [default].
65
56
*/
66
57
public infix fun <V , E > Result <V , E >.getErrorOr (default : E ): E {
67
- return when ( this ) {
68
- is Ok -> default
69
- is Err -> error
58
+ return when {
59
+ isOk -> default
60
+ else -> error
70
61
}
71
62
}
72
63
73
64
/* *
74
- * Returns the [value][Ok .value] if this [Result] is [Ok ], otherwise the
75
- * [transformation][transform] of the [error][Err .error].
65
+ * Returns the [value][Result .value] if this result [ is ok][Result.isOk ], otherwise the
66
+ * [transformation][transform] of the [error][Result .error].
76
67
*
77
68
* - Elm: [Result.extract](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#extract)
78
69
* - Rust: [Result.unwrap_or_else](https://doc.rust-lang.org/src/core/result.rs.html#735-740)
@@ -82,69 +73,64 @@ public inline infix fun <V, E> Result<V, E>.getOrElse(transform: (E) -> V): V {
82
73
callsInPlace(transform, InvocationKind .AT_MOST_ONCE )
83
74
}
84
75
85
- return when ( this ) {
86
- is Ok -> value
87
- is Err -> transform(error)
76
+ return when {
77
+ isOk -> value
78
+ else -> transform(error)
88
79
}
89
80
}
90
81
91
82
/* *
92
- * Returns the [error][Err .error] if this [Result] is [Err ], otherwise the
93
- * [transformation][transform] of the [value][Ok .value].
83
+ * Returns the [error][Result .error] if this result [ is an error][Result.isErr ], otherwise the
84
+ * [transformation][transform] of the [value][Result .value].
94
85
*/
95
86
public inline infix fun <V , E > Result <V , E >.getErrorOrElse (transform : (V ) -> E ): E {
96
87
contract {
97
88
callsInPlace(transform, InvocationKind .AT_MOST_ONCE )
98
89
}
99
90
100
- return when ( this ) {
101
- is Ok -> transform(value)
102
- is Err -> error
91
+ return when {
92
+ isErr -> error
93
+ else -> transform(value)
103
94
}
104
95
}
105
96
106
97
/* *
107
- * Returns the [value][Ok .value] if this [Result] is [Ok ], otherwise throws the
108
- * [error][Err .error].
98
+ * Returns the [value][Result .value] if this result [ is ok][Result.isOk ], otherwise throws the
99
+ * [error][Result .error].
109
100
*
110
101
* This is functionally equivalent to [`getOrElse { throw it }`][getOrElse].
111
102
*/
112
103
public fun <V , E : Throwable > Result <V , E >.getOrThrow (): V {
113
- contract {
114
- returns() implies (this @getOrThrow is Ok <V >)
115
- }
116
-
117
- return when (this ) {
118
- is Ok -> value
119
- is Err -> throw error
104
+ return when {
105
+ isOk -> value
106
+ else -> throw error
120
107
}
121
108
}
122
109
123
110
/* *
124
- * Returns the [value][Ok .value] if this [Result] is [Ok ], otherwise throws the
125
- * [transformation][transform] of the [error][Err .error] to a [Throwable].
111
+ * Returns the [value][Result .value] if this result [ is ok][Result.isOk ], otherwise throws the
112
+ * [transformation][transform] of the [error][Result .error] to a [Throwable].
126
113
*/
127
114
public inline infix fun <V , E > Result <V , E >.getOrThrow (transform : (E ) -> Throwable ): V {
128
115
contract {
129
- returns() implies (this @getOrThrow is Ok <V >)
130
116
callsInPlace(transform, InvocationKind .AT_MOST_ONCE )
131
117
}
132
118
133
- return when ( this ) {
134
- is Ok -> value
135
- is Err -> throw transform(error)
119
+ return when {
120
+ isOk -> value
121
+ else -> throw transform(error)
136
122
}
137
123
}
138
124
139
125
/* *
140
- * Merges this [Result<V, E>][Result] to [U], returning the [value][Ok .value] if this [Result] is [Ok], otherwise the
141
- * [error][Err .error].
126
+ * Merges this [Result<V, E>][Result] to [U], returning the [value][Result .value] if this result
127
+ * [is ok][Result.isOk], otherwise the [ error][Result .error].
142
128
*
143
129
* - Scala: [MergeableEither.merge](https://www.scala-lang.org/api/2.12.0/scala/util/Either$$MergeableEither.html#merge:A)
144
130
*/
145
131
public fun <V : U , E : U , U > Result <V , E >.merge (): U {
146
- return when ( this ) {
147
- is Ok -> value
148
- is Err -> error
132
+ return when {
133
+ isOk -> value
134
+ else -> error
149
135
}
150
136
}
0 commit comments