and Then Of
Transforms the encapsulated value if this instance represents success, wrapping the success lambda in resultOf so that any thrown exception is re-encapsulated as a failure rather than propagating.
Unlike Result.map, exceptions from success are caught. Unlike Result.mapCatching, CancellationException is re-thrown when the coroutine is no longer active.
Failures pass through unchanged.
resultOf { 4 }
.andThenOf { it * 2 } // Result.success(8)
.andThenOf { check(false) { it } } // Result.failure(IllegalStateException("8"))
.andThenOf { 16 } // Remains Result.failure(IllegalStateException("8"))Content copied to clipboard
Parameters
I
The encapsulated success input type.
O
The output type after transformation.
See also
inline suspend fun <T> KotlinResult<T>.andThenOf(predicate: (T) -> Boolean, success: (T) -> T): KotlinResult<T>
Conditionally transforms the encapsulated value if success and predicate returns true. If predicate returns false, the value is left unchanged.
Exceptions from success are caught by resultOf. Failures pass through unchanged.
resultOf { 4 }.andThenOf({ it > 0 }) { it * 2 } // Result.success(8)
resultOf { 4 }.andThenOf({ it < 0 }) { it * 2 } // Result.success(4)Content copied to clipboard