-
Notifications
You must be signed in to change notification settings - Fork 21
for comprehension loses backticks when desugaring to val? #11533
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
Maybe it desugars to a pattern match? Typer says: private[this] val x: Option[Nothing] = scala.Option.apply[Int](3).flatMap[Nothing](((F(Y|Z): Int) => Option(6).map(((x$3: Int) => x$3: @scala.unchecked match {
case (x$1 @ `P$u0028X$barC$u0029`) => {
val <x$2: error>: <error> = 4: @scala.unchecked match {
case (<x$2: error> @ `<A$u0028B$barC$u0029: error>`) => <x$2: error>
};
scala.Tuple2(x$1, <x$2: error>)
}
})).map(((<x$4: error>: <error>) => <x$4: error>: @scala.unchecked match {
case scala.Tuple2(`P$u0028X$barC$u0029`, `<A$u0028B$barC$u0029: error>`) => `P$u0028X$barC$u0029`
})))); |
The spec is vague on this topic. The only thing I'm sure about is that something is wrong. Starting with regular value definitions (https://www.scala-lang.org/files/archive/spec/2.12/04-basic-declarations-and-definitions.html#value-declarations-and-definitions). The formal syntax for the LHS is always at least a
There is no formal definition of "simple name" in the spec. According to use of the term in earlier chapters it refers to an unqualified identifier which may also be "an arbitrary string between back-quotes". The implementation of value definitions agrees with this. Any unqualified identifier, even when enclosed in backquotes, is treated as a simple name. No pattern matching applies. According to https://www.scala-lang.org/files/archive/spec/2.12/06-expressions.html#for-comprehensions-and-for-loops:
This could be interpreted to mean that This brings us to https://www.scala-lang.org/files/archive/spec/2.12/08-pattern-matching.html#stable-identifier-patterns:
It does not say anything about other identifiers in backquotes. In practice, all identifiers in backquotes are interpreted as stable identifier patterns in proper patterns (i.e. not ones that are treated as simple names). But since generators in As the original bug report in this ticket shows, value definitions in By the way, when it comes to generators, there are other oddities. Take this simple typed pattern:
It should desugar to:
But actually it becomes
which doesn't compile. It looks like the compiler is isn't quite sure whether it's a typed assignment or a typed pattern. |
They are all patterns. It seems to me the problem is with the other generators; the back-ticked pattern is refutable. |
Syntactically they are all patterns, but as I elaborated (far too long) above, that's not sufficient for deciding on their interpretation. Syntactically, Refutability does not matter at that point. If you see |
I think you're overthinking it. The syntax to introduce a funky name is:
which corresponds to this syntax which was enhanced in 2.12, because previously it had to be a varid:
Ordinary val def syntax to introduce a name is not a pattern, irrespective of the productions. The text says the LHS can also be a pattern, etc. Of course this leads to differing expectations about what the |
for (`funky name` <- List(27))
Does introduce a funky name too though, which might be the bug.
…On Fri, May 17, 2019, 18:38 som-snytt ***@***.***> wrote:
I think you're overthinking it. The syntax to introduce a funky name is:
for (`funky name` @ _ <- List(27))
which corresponds to this syntax which was enhanced in 2.12, because
previously it had to be a varid:
27 match { case `funky name` @ _ => `funky name` }
Ordinary val def syntax to introduce a name is not a pattern, irrespective
of the productions.
The text says the LHS can also be a pattern, etc. Of course this leads to
differing expectations about what the x: String syntax does in a
generator (filtering or throwing or compile error, I don't even know what
it does without testing).
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#11533?email_source=notifications&email_token=AAGXOEM67TMTHOQNQZYLK43PV3NPNA5CNFSM4HNCRW3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVVIADI#issuecomment-493518861>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAGXOEORVYZKQ45YCIGSPULPV3NPNANCNFSM4HNCRW3A>
.
|
I prefer to call this "others are underspecifying it" |
@martijnhoekstra Yes, IMHO, that is the bug because it should be taken as a pattern. But I wouldn't be averse to rewriting the rules, either, or clarifying them the other way. The umbrella issue is #900 which reminds me, I have a PR to revive that helps with retronym's workaround, to use |
Scala 3:
Scala 2 attempts to be helpful in its own way:
Note that Scala 3 is now aligned with Scala 2 in accepting
Scala 3 adds
where Scala 2 is Scala 3 clarifies the "generator" case by adding
similarly
Scala 2 is stuck at unhelpful desugaring as shown above
(It should always rewrite to cases, which will synthesize the correct function.) On "simple name": "simple" is an Oderskyism, as in "Simply Scala". In the recent doc for givens, a reviewer requested he revise "simple" to "mere". "If the given is a mere alias." An example from Scala 3 where an assignment can only be construed as a pattern is
A def may say |
This arose from this thread on Scala Users.
Take the following code (working fiddle, Scala 2.12):
Everything compiles for me except the specified line, which is getting the error:
All of this suggests to me that something's a bit weird with backtick'ed identifiers when desugaring to a
val
. The desugars toflatMap
andmap
appear to work, but the one toval
doesn't...The text was updated successfully, but these errors were encountered: