-
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
In 3.4 make refutable patterns in a for comprehension an error #18842
In 3.4 make refutable patterns in a for comprehension an error #18842
Conversation
Do this PR supports the direct tuple assignment in a for comprehension? |
not sure quite what you mean, but e.g. for (a, b) <- ((1 to 10) zip ('a' to 'z'))
yield s"$a$b" will desugar to ((1 to 10) zip ('a' to 'z'))
.map((a, b) => s"$a$b") instead of ((1 to 10) zip ('a' to 'z'))
.withFilter({ case (a, b) => true; case _ => false })
.map((a, b) => s"$a$b") |
@@ -38,6 +22,22 @@ | |||
| If the narrowing is intentional, this can be communicated by adding the `case` keyword before the full pattern, | |||
| which will result in a filtering for expression (using `withFilter`). | |||
| This patch can be rewritten automatically under -rewrite -source 3.2-migration. | |||
-- Error: tests/neg/refutable-pattern-binding-messages.scala:5:14 ------------------------------------------------------ | |||
5 | val Positive(p) = 5 // error: refutable extractor |
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.
Why did these error messages move? It seems non-optimal, as the messages are not in source order anymore.
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.
all that was changed was the warning became an error, so perhaps under -Werror
true errors print last?
I mean, with this we could write: def z = for
(a, b) <- ZIO.fromOption(Some(1, 2))
yield (a, b) Like the behavior with "-source:future" instead of assigning the result first to a variable and then unpack the tuple: def y = for
x <- ZIO.fromOption(Some(1, 2))
(a, b) = x
yield (a, b)
|
d10a4b1
to
0bfd343
Compare
yes this PR makes the pattern checking for comprehensions behave like |
supersedes #16665
Only make refutable patterns in a for comprehension an error, here we have a clear set in stone solution: put
case
before the pattern.It is still in the air the ideal solution for pattern val definitions, see https://contributors.scala-lang.org/t/pre-sip-replace-non-sensical-unchecked-annotations/6342/85, so keep those as a warning for now
Release notes
In 3.4 refutable patterns (i.e. patterns that might not match) in a for-comprehension generator must now be preceded by
case
, or else an error is reported.e.g.
.withFilter
is also no longer inserted for a pattern in a generator unless prefixed bycase
, meaning improved ergonomics for many types that do not implement.withFilter