andIf

inline fun <T> KotlinResult<T>.andIf(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 result. Do not use in suspend contextsCancellationException will be caught.

Failures pass through unchanged.

result { 4 }.andIf({ it > 0 }) { it * 2 } // Result.success(8)
result { 4 }.andIf({ it < 0 }) { it * 2 } // Result.success(4)

See also