-
Notifications
You must be signed in to change notification settings - Fork 451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix filterOrElse to handle null values correctly. #2933
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch 🙏 Thank you for raising the issue, and so quickly following up with a PR for a fix! 😍
Congrats on your first contribution as well 🙌
We should keep this issue open since this mistake is likely also made in Option
and potentially Ior
. Luckily this is not released yet.
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()) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nomisRev - on a related note we defined rightIfNotNull
, rightIfNull
, leftIfNull
in the app itself since we found it useful for the expressiveness / readability.
Going to merge the PR, thanks for your contribution and quick action @bwanner 🙏
We're always open to keep them around in Arrow if they're being used often. It's the second time I hear about either { // In the next minor version (with automatic migration) you won't need `suspend/eager` anymore.
// rightIfNull
ensure(this == null) { error() }
// rightIfNotNull
ensureNotNull(this) { error() } // this is smart-casted to non-null after this point.
// leftIfNull
val b = either.bind()
ensureNotNull(b) error() } // b is smart-casted to non-null after this point.
} If find To keep this more in line with the DSL API we could add following APIs:
At least in this case we could add the same APIs for |
@nomisRev I agree that just
I like the idea of adding |
PR for issue #2932