-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Optional braces not allowed for pattern match guard #11444
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
Comments
The syntax says:
From the spec, it seems to be allowed. But such code in practice is not readable, we may also align the spec to reject the code (if the fix to the syntax is simple). The reason why the parser does not see the block is that in parsing |
Here is the rule for inserting indentations:
The rule above says that |
Yeah, it seems to be allow in a guard. I already reject it in the scalameta parser, but would love to have some clarification on this. |
The following code is valid (compiles without problem): 1 match {
case a
if
val b = 11
a < b
=>
a
} The In contrast, in the following code: 1 match {
case a if
val b = 11
a < b =>
a
} The Related code: |
@odersky is the two different |
Reading https://www.scala-lang.org/blog/2021/09/07/scala-3.0.2-released.html:
I was hoping that this variant to the original test case would now work:
But sadly you still get:
|
The following would work:
And this works as well:
The reason the first example does not work is that there is an implicit opening region where the pattern I don't think there's anything we can reasonably do here. |
If we can't find the indentation of the enclosing region, shouldn't we just use the one enclosing it? Are we saying that:
will just nor work? I don't think this is quite legible for the users and it feels like we have a bunch of hidden indentation rules or at least difficult ones that are not exactly user friendly. |
I recently became a fan of aligning I'm glad to see that I can go to town left-adjusting the pattern. For comprehensions offer less room for personal expression, but generator syntax is pretty generous.
That's just exploiting leading infix. This outdents as expected:
with an interesting message that sounds like the converse of the usual unindent unexpected
|
My recent remotely related question at #14738 (comment) was whether the example on that ticket
should already use "enclosing" indentation so this would be normal syntax
but I haven't thought about it again and in fact I've already forgotten everything I learned about indentation rules for optional braces, so it's nice that they mostly just work. |
That's generally not how it works. One problem would be to decide when a region ends. If there are several reasons with the same indentation level that's an issue. So that's one reason why it is generally not done. |
Compiler version
3.0.0-RC1
Minimized code
Output
Expectation
Compiles succesfully.
It seems this works in both normal ifs and if in for comprehensions, so this seems a bit of an inconsistency.
The text was updated successfully, but these errors were encountered: