and If
inline fun <Ok : Any, Error : Any> Outcome<Ok, Error>.andIf(predicate: (Ok) -> Boolean, catch: (throwable: Throwable) -> Outcome<Ok, Error> = ::rethrow, success: RaiseScope<Error>.(Ok) -> Ok): Outcome<Ok, Error>
Transforms the encapsulated value if this instance represents success, and the predicate returns true. If predicate is false, the Outcome.Success is left unchanged.
If success throws an exception, it will be re-encapsulated or re-thrown by outcomeOf. If no exception occurs, or Outcome is failure, the original Outcome is returned unaffected.
outcomeOf { 4 }.andIf({ it 0 }) { it * 2 } // Outcome.success(8)
outcomeOf { 4 }.andIf({ it < 0 }) { it * 2 } // Outcome.success(4)
Content copied to clipboard