Skip to content

Commit

Permalink
Fix filterOrElse to handle null values correctly. (#2933)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Wanner <bwanner@netflix.com>
  • Loading branch information
bwanner and Ben Wanner authored Feb 17, 2023
1 parent 878301a commit 1d6a6aa
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2069,10 +2069,10 @@ public inline fun <A, B> Either<A, B>.getOrHandle(default: (A) -> B): B =
*/
@Deprecated(
RedundantAPI + "Prefer if-else statement inside either DSL, or replace with explicit flatMap",
ReplaceWith("flatMap { b -> b.takeIf(predicate)?.right() ?: default().left() }")
ReplaceWith("flatMap { if (predicate(it)) Right(it) else Left(default(it)) }")
)
public inline fun <A, B> Either<A, B>.filterOrElse(predicate: (B) -> Boolean, default: () -> A): Either<A, B> =
ensure(default, predicate)
flatMap { if (predicate(it)) Right(it) else Left(default()) }

/**
* Returns [Right] with the existing value of [Right] if this is a [Right] and the given
Expand Down

0 comments on commit 1d6a6aa

Please sign in to comment.