@@ -12,13 +12,13 @@ import kotlin.contracts.contract
12
12
* A discriminated union that encapsulates a successful outcome with a value of type [T]
13
13
* or a failure with an arbitrary [Throwable] exception.
14
14
*/
15
- sealed class Result <out T > : Serializable {
15
+ sealed class SealedResult <out T > : Serializable {
16
16
17
17
@PublishedApi
18
- internal data class Failure <out T >(val exception : Throwable ) : Result <T>()
18
+ internal data class Failure <out T >(val exception : Throwable ) : SealedResult <T>()
19
19
20
20
@PublishedApi
21
- internal data class Success <out T >(val value : T ) : Result <T>()
21
+ internal data class Success <out T >(val value : T ) : SealedResult <T>()
22
22
23
23
/* *
24
24
* Returns `true` if this instance represents a failed outcome.
@@ -35,8 +35,8 @@ sealed class Result<out T> : Serializable {
35
35
get() = this is Success
36
36
37
37
/* *
38
- * Returns the encapsulated value if this instance represents [success][Result .isSuccess] or `null`
39
- * if it is [failure][Result .isFailure].
38
+ * Returns the encapsulated value if this instance represents [success][SealedResult .isSuccess] or `null`
39
+ * if it is [failure][SealedResult .isFailure].
40
40
*
41
41
* This function is a shorthand for `getOrElse { null }` (see [getOrElse]) or
42
42
* `fold(onSuccess = { it }, onFailure = { null })` (see [fold]).
@@ -58,7 +58,7 @@ sealed class Result<out T> : Serializable {
58
58
}
59
59
60
60
/* *
61
- * Returns a string `Success(v)` if this instance represents [success][Result .isSuccess]
61
+ * Returns a string `Success(v)` if this instance represents [success][SealedResult .isSuccess]
62
62
* where `v` is a string representation of the value or a string `Failure(x)` if
63
63
* it is [failure][isFailure] where `x` is a string representation of the exception.
64
64
*/
@@ -68,20 +68,20 @@ sealed class Result<out T> : Serializable {
68
68
}
69
69
70
70
/* *
71
- * Companion object for [Result ] class that contains its constructor functions
71
+ * Companion object for [SealedResult ] class that contains its constructor functions
72
72
* [success] and [failure].
73
73
*/
74
74
public companion object {
75
75
76
76
/* *
77
77
* Returns an instance that encapsulates the given [value] as successful value.
78
78
*/
79
- public fun <T > success (value : T ): Result <T > = Success (value)
79
+ public fun <T > success (value : T ): SealedResult <T > = Success (value)
80
80
81
81
/* *
82
82
* Returns an instance that encapsulates the given [Throwable] [exception] as failure.
83
83
*/
84
- public fun <T > failure (exception : Throwable ): Result <T > = Failure (exception)
84
+ public fun <T > failure (exception : Throwable ): SealedResult <T > = Failure (exception)
85
85
}
86
86
}
87
87
@@ -90,67 +90,67 @@ sealed class Result<out T> : Serializable {
90
90
* catching any [Throwable] exception that was thrown from the [block] function execution and encapsulating it as a failure.
91
91
*/
92
92
@Suppress(" FunctionName" , " TooGenericExceptionCaught" )
93
- public inline fun <T > Result (block : () -> T ): Result <T > {
93
+ public inline fun <T > SealedResult (block : () -> T ): SealedResult <T > {
94
94
return try {
95
- Result .success(block())
95
+ SealedResult .success(block())
96
96
} catch (exception: Throwable ) {
97
- Result .failure(exception)
97
+ SealedResult .failure(exception)
98
98
}
99
99
}
100
100
101
101
/* *
102
- * Returns the encapsulated value if this instance represents [success][Result .isSuccess] or throws the encapsulated [Throwable] exception
103
- * if it is [failure][Result .isFailure].
102
+ * Returns the encapsulated value if this instance represents [success][SealedResult .isSuccess] or throws the encapsulated [Throwable] exception
103
+ * if it is [failure][SealedResult .isFailure].
104
104
*
105
105
* This function is a shorthand for `getOrElse { throw it }` (see [getOrElse]).
106
106
*/
107
- public fun <T > Result <T>.getOrThrow (): T {
107
+ public fun <T > SealedResult <T>.getOrThrow (): T {
108
108
return when (this ) {
109
- is Result .Failure -> throw exception
110
- is Result .Success -> value
109
+ is SealedResult .Failure -> throw exception
110
+ is SealedResult .Success -> value
111
111
}
112
112
}
113
113
114
114
/* *
115
- * Returns the encapsulated value if this instance represents [success][Result .isSuccess] or the
116
- * result of [onFailure] function for the encapsulated [Throwable] exception if it is [failure][Result .isFailure].
115
+ * Returns the encapsulated value if this instance represents [success][SealedResult .isSuccess] or the
116
+ * result of [onFailure] function for the encapsulated [Throwable] exception if it is [failure][SealedResult .isFailure].
117
117
*
118
118
* Note, that this function rethrows any [Throwable] exception thrown by [onFailure] function.
119
119
*
120
120
* This function is a shorthand for `fold(onSuccess = { it }, onFailure = onFailure)` (see [fold]).
121
121
*/
122
122
@OptIn(ExperimentalContracts ::class )
123
- public inline fun <R , T : R > Result <T>.getOrElse (onFailure : (exception: Throwable ) -> R ): R {
123
+ public inline fun <R , T : R > SealedResult <T>.getOrElse (onFailure : (exception: Throwable ) -> R ): R {
124
124
contract {
125
125
callsInPlace(onFailure, InvocationKind .AT_MOST_ONCE )
126
126
}
127
127
return when (this ) {
128
- is Result .Failure -> onFailure(exception)
129
- is Result .Success -> value
128
+ is SealedResult .Failure -> onFailure(exception)
129
+ is SealedResult .Success -> value
130
130
}
131
131
}
132
132
133
133
/* *
134
- * Returns the encapsulated value if this instance represents [success][Result .isSuccess] or the
135
- * [defaultValue] if it is [failure][Result .isFailure].
134
+ * Returns the encapsulated value if this instance represents [success][SealedResult .isSuccess] or the
135
+ * [defaultValue] if it is [failure][SealedResult .isFailure].
136
136
*
137
137
* This function is a shorthand for `getOrElse { defaultValue }` (see [getOrElse]).
138
138
*/
139
- public fun <R , T : R > Result <T>.getOrDefault (defaultValue : R ): R {
139
+ public fun <R , T : R > SealedResult <T>.getOrDefault (defaultValue : R ): R {
140
140
return when (this ) {
141
- is Result .Failure -> defaultValue
142
- is Result .Success -> value
141
+ is SealedResult .Failure -> defaultValue
142
+ is SealedResult .Success -> value
143
143
}
144
144
}
145
145
146
146
/* *
147
- * Returns the result of [onSuccess] for the encapsulated value if this instance represents [success][Result .isSuccess]
148
- * or the result of [onFailure] function for the encapsulated [Throwable] exception if it is [failure][Result .isFailure].
147
+ * Returns the result of [onSuccess] for the encapsulated value if this instance represents [success][SealedResult .isSuccess]
148
+ * or the result of [onFailure] function for the encapsulated [Throwable] exception if it is [failure][SealedResult .isFailure].
149
149
*
150
150
* Note, that this function rethrows any [Throwable] exception thrown by [onSuccess] or by [onFailure] function.
151
151
*/
152
152
@OptIn(ExperimentalContracts ::class )
153
- public inline fun <R , T > Result <T>.fold (
153
+ public inline fun <R , T > SealedResult <T>.fold (
154
154
onFailure : (exception: Throwable ) -> R ,
155
155
onSuccess : (value: T ) -> R
156
156
): R {
@@ -159,148 +159,148 @@ public inline fun <R, T> Result<T>.fold(
159
159
callsInPlace(onSuccess, InvocationKind .AT_MOST_ONCE )
160
160
}
161
161
return when (this ) {
162
- is Result .Failure -> onFailure(exception)
163
- is Result .Success -> onSuccess(value)
162
+ is SealedResult .Failure -> onFailure(exception)
163
+ is SealedResult .Success -> onSuccess(value)
164
164
}
165
165
}
166
166
167
167
/* *
168
168
* Returns the encapsulated result of the given [transform] function applied to the encapsulated value
169
- * if this instance represents [success][Result .isSuccess] or the
170
- * original encapsulated [Throwable] exception if it is [failure][Result .isFailure].
169
+ * if this instance represents [success][SealedResult .isSuccess] or the
170
+ * original encapsulated [Throwable] exception if it is [failure][SealedResult .isFailure].
171
171
*
172
172
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
173
173
* See [mapCatching] for an alternative that encapsulates exceptions.
174
174
*/
175
175
@OptIn(ExperimentalContracts ::class )
176
- public inline fun <R , T > Result <T>.map (transform : (value: T ) -> R ): Result <R > {
176
+ public inline fun <R , T > SealedResult <T>.map (transform : (value: T ) -> R ): SealedResult <R > {
177
177
contract {
178
178
callsInPlace(transform, InvocationKind .AT_MOST_ONCE )
179
179
}
180
180
return when (this ) {
181
- is Result .Failure -> Result .failure(exception)
182
- is Result .Success -> Result .success(transform(value))
181
+ is SealedResult .Failure -> SealedResult .failure(exception)
182
+ is SealedResult .Success -> SealedResult .success(transform(value))
183
183
}
184
184
}
185
185
186
186
/* *
187
187
* Returns the encapsulated result of the given [transform] function applied to the encapsulated value
188
- * if this instance represents [success][Result .isSuccess] or the
189
- * original encapsulated [Throwable] exception if it is [failure][Result .isFailure].
188
+ * if this instance represents [success][SealedResult .isSuccess] or the
189
+ * original encapsulated [Throwable] exception if it is [failure][SealedResult .isFailure].
190
190
*
191
191
* This function catches any [Throwable] exception thrown by [transform] function and encapsulates it as a failure.
192
192
* See [map] for an alternative that rethrows exceptions from `transform` function.
193
193
*/
194
- public inline fun <R , T > Result <T>.mapCatching (transform : (value: T ) -> R ): Result <R > {
194
+ public inline fun <R , T > SealedResult <T>.mapCatching (transform : (value: T ) -> R ): SealedResult <R > {
195
195
return when (this ) {
196
- is Result .Failure -> Result .failure(exception)
197
- is Result .Success -> Result { transform(value) }
196
+ is SealedResult .Failure -> SealedResult .failure(exception)
197
+ is SealedResult .Success -> SealedResult { transform(value) }
198
198
}
199
199
}
200
200
201
201
/* *
202
- * Returns a [Result ] of the given [transform] function applied to the encapsulated value
203
- * if this instance represents [success][Result .isSuccess] or the
204
- * original encapsulated [Throwable] exception if it is [failure][Result .isFailure].
202
+ * Returns a [SealedResult ] of the given [transform] function applied to the encapsulated value
203
+ * if this instance represents [success][SealedResult .isSuccess] or the
204
+ * original encapsulated [Throwable] exception if it is [failure][SealedResult .isFailure].
205
205
*
206
206
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
207
207
* See [flatMapCatching] for an alternative that encapsulates exceptions.
208
208
*/
209
209
@OptIn(ExperimentalContracts ::class )
210
- public inline fun <R , T > Result <T>.flatMap (transform : (value: T ) -> Result <R >): Result <R > {
210
+ public inline fun <R , T > SealedResult <T>.flatMap (transform : (value: T ) -> SealedResult <R >): SealedResult <R > {
211
211
contract {
212
212
callsInPlace(transform, InvocationKind .AT_MOST_ONCE )
213
213
}
214
214
return when (this ) {
215
- is Result .Failure -> Result .failure(exception)
216
- is Result .Success -> transform(value)
215
+ is SealedResult .Failure -> SealedResult .failure(exception)
216
+ is SealedResult .Success -> transform(value)
217
217
}
218
218
}
219
219
220
220
/* *
221
- * Returns a [Result ] of the given [transform] function applied to the encapsulated value
222
- * if this instance represents [success][Result .isSuccess] or the
223
- * original encapsulated [Throwable] exception if it is [failure][Result .isFailure].
221
+ * Returns a [SealedResult ] of the given [transform] function applied to the encapsulated value
222
+ * if this instance represents [success][SealedResult .isSuccess] or the
223
+ * original encapsulated [Throwable] exception if it is [failure][SealedResult .isFailure].
224
224
*
225
225
* This function catches any [Throwable] exception thrown by [transform] function and encapsulates it as a failure.
226
226
* See [map] for an alternative that rethrows exceptions from `transform` function.
227
227
*/
228
- public inline fun <R , T > Result <T>.flatMapCatching (transform : (value: T ) -> Result <R >): Result <R > {
228
+ public inline fun <R , T > SealedResult <T>.flatMapCatching (transform : (value: T ) -> SealedResult <R >): SealedResult <R > {
229
229
return when (this ) {
230
- is Result .Failure -> Result .failure(exception)
231
- is Result .Success -> Result { transform(value).getOrThrow() }
230
+ is SealedResult .Failure -> SealedResult .failure(exception)
231
+ is SealedResult .Success -> SealedResult { transform(value).getOrThrow() }
232
232
}
233
233
}
234
234
235
235
/* *
236
236
* Returns the encapsulated result of the given [transform] function applied to the encapsulated [Throwable] exception
237
- * if this instance represents [failure][Result .isFailure] or the
238
- * original encapsulated value if it is [success][Result .isSuccess].
237
+ * if this instance represents [failure][SealedResult .isFailure] or the
238
+ * original encapsulated value if it is [success][SealedResult .isSuccess].
239
239
*
240
240
* Note, that this function rethrows any [Throwable] exception thrown by [transform] function.
241
241
* See [recoverCatching] for an alternative that encapsulates exceptions.
242
242
*/
243
243
@OptIn(ExperimentalContracts ::class )
244
- public inline fun <R , T : R > Result <T>.recover (transform : (exception: Throwable ) -> R ): Result <R > {
244
+ public inline fun <R , T : R > SealedResult <T>.recover (transform : (exception: Throwable ) -> R ): SealedResult <R > {
245
245
contract {
246
246
callsInPlace(transform, InvocationKind .AT_MOST_ONCE )
247
247
}
248
248
return when (this ) {
249
- is Result .Failure -> Result .success(transform(exception))
250
- is Result .Success -> this
249
+ is SealedResult .Failure -> SealedResult .success(transform(exception))
250
+ is SealedResult .Success -> this
251
251
}
252
252
}
253
253
254
254
/* *
255
255
* Returns the encapsulated result of the given [transform] function applied to the encapsulated [Throwable] exception
256
- * if this instance represents [failure][Result .isFailure] or the
257
- * original encapsulated value if it is [success][Result .isSuccess].
256
+ * if this instance represents [failure][SealedResult .isFailure] or the
257
+ * original encapsulated value if it is [success][SealedResult .isSuccess].
258
258
*
259
259
* This function catches any [Throwable] exception thrown by [transform] function and encapsulates it as a failure.
260
260
* See [recover] for an alternative that rethrows exceptions.
261
261
*/
262
- public inline fun <R , T : R > Result <T>.recoverCatching (transform : (exception: Throwable ) -> R ): Result <R > {
262
+ public inline fun <R , T : R > SealedResult <T>.recoverCatching (transform : (exception: Throwable ) -> R ): SealedResult <R > {
263
263
return when (this ) {
264
- is Result .Failure -> Result { transform(exception) }
265
- is Result .Success -> this
264
+ is SealedResult .Failure -> SealedResult { transform(exception) }
265
+ is SealedResult .Success -> this
266
266
}
267
267
}
268
268
269
269
/* *
270
- * Performs the given [action] on the encapsulated [Throwable] exception if this instance represents [failure][Result .isFailure].
270
+ * Performs the given [action] on the encapsulated [Throwable] exception if this instance represents [failure][SealedResult .isFailure].
271
271
* Returns the original `Result` unchanged.
272
272
*/
273
273
@OptIn(ExperimentalContracts ::class )
274
- public inline fun <T > Result <T>.onFailure (action : (exception: Throwable ) -> Unit ): Result <T > {
274
+ public inline fun <T > SealedResult <T>.onFailure (action : (exception: Throwable ) -> Unit ): SealedResult <T > {
275
275
contract {
276
276
callsInPlace(action, InvocationKind .AT_MOST_ONCE )
277
277
}
278
- if (this is Result .Failure ) action(exception)
278
+ if (this is SealedResult .Failure ) action(exception)
279
279
return this
280
280
}
281
281
282
282
/* *
283
- * Performs the given [action] on the encapsulated value if this instance represents [success][Result .isSuccess].
283
+ * Performs the given [action] on the encapsulated value if this instance represents [success][SealedResult .isSuccess].
284
284
* Returns the original `Result` unchanged.
285
285
*/
286
286
@OptIn(ExperimentalContracts ::class )
287
- public inline fun <T > Result <T>.onSuccess (action : (value: T ) -> Unit ): Result <T > {
287
+ public inline fun <T > SealedResult <T>.onSuccess (action : (value: T ) -> Unit ): SealedResult <T > {
288
288
contract {
289
289
callsInPlace(action, InvocationKind .AT_MOST_ONCE )
290
290
}
291
- if (this is Result .Success ) action(value)
291
+ if (this is SealedResult .Success ) action(value)
292
292
return this
293
293
}
294
294
295
295
@OptIn(ExperimentalContracts ::class )
296
- fun <T > Flow<Result <T>>.onResult (
296
+ fun <T > Flow<SealedResult <T>>.onResult (
297
297
onFailure : suspend (exception: Throwable ) -> Unit ,
298
298
onSuccess : suspend (value: T ) -> Unit
299
- ): Flow <Result <T >> {
299
+ ): Flow <SealedResult <T >> {
300
300
return onEach { result ->
301
301
when (result) {
302
- is Result .Failure -> onFailure(result.exception)
303
- is Result .Success -> onSuccess(result.value)
302
+ is SealedResult .Failure -> onFailure(result.exception)
303
+ is SealedResult .Success -> onSuccess(result.value)
304
304
}
305
305
}
306
306
}
0 commit comments