and If
inline fun <T> KotlinResult<T>.andIf(predicate: (T) -> Boolean, onSuccess: (T) -> T): KotlinResult<T>
If the predicate is true
, transforms value inside a resultOf scope.
Success ->wraps&
returns
the result of onSuccess transformation.If onSuccess throws an exception, it will be caught & wrapped by resultOf.
resultOf { 4 }.andIf({ it 0 }) { it * 2 } // KotlinResult.success(8)
resultOf { 4 }.andIf({ it < 0 }) { it * 2 } // KotlinResult.success(4)
Content copied to clipboard