-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Pattern decomposition failing in for comprehension for Either #17216
Comments
Current workaround with an extension like so: object IOOps {
extension [A](op: IO[A])
def withFilter(func: A => Boolean): IO[A] = op
} |
Can you provide a minimization without the cats dependency? |
@soronpo Sure. The below will not compile, as tuple decomposition in for comprehensions also fails with Either or any other class that lacks the val someEitherOp: Either[Exception, (Int, String)] = Right((1,"sample"))
for {
(num, str) <- someEitherOp
} yield () Interesting note: Option possesses the someEitherOp.flatMap((num, _) => Right(num))
someEitherOp.map((num, _) => num)
|
Seeing this too, with Either, in 3.3.0. |
@anatoliykmetyuk , can we have an update on this? |
The documentation of https://www.scala-lang.org/api/2.13.6/scala/util/Either.html states that // Guard expressions are not supported:
for {
i <- right1
if i > 0
} yield i
// error: value withFilter is not a member of Right[Double,Int]
// Similarly, refutable patterns are not supported:
for (x: Int <- right1) yield x
// error: value withFilter is not a member of Right[Double,Int] which probably also covers this case. I assume that there might be a fundamental reason for this to not be implemented in the standard library. If not this is something that would need to change in the Scala 2 standard library directly. |
@nicolasstucki , thanks for the link, as it's helpful to show some of the issues that Altered Code:val right1: Either[Exception, (Int, String)] = Right((1,"sample"))
right1.flatMap(res => {
if (res._1 > 0) {
res
}
}) OutputFound: Unit
Required: Either[Any, Any]
Maybe you are missing an else part for the conditional?
if (res._1 > 0) { |
@nicolasstucki , in short, because tuple decomposition does work in a |
That for comprehension desugars into a The spec explicitly states
Therefore it is not a bug. |
Maybe you should just use def test(either: Either[Exception, (Int, String)]) =
for {
- (num, str) <- either
+ (num, str) <- either.toOption
} yield () |
There is discussion of In short, you need a Deprecated |
@nicolasstucki , a few questions:
|
Not sure. There are some concerns about laziness in the
This issue is that |
@nicolasstucki , since Scala 2.12
|
Do we actually still need this issue in the presence of |
Compiler version
3.2.0. Also tried 3.3.0-RC2 and found the same bug.
Cats Effect version
3.4.8
Minimized code
Output
Expectation
Expected that decomposition would succeed inside the for comprehension and that
num
andstr
would be parsed toInt
andString
respectively. The compiler parses a normal flatMap call correctly. So the following correctly returns the number:It's just decomposition inside the for comprehension that fails for the IO. This bug does not affect Option, but it does affect Either, since Either lacks the
.withFilter
method.The text was updated successfully, but these errors were encountered: