-
Notifications
You must be signed in to change notification settings - Fork 205
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
Should we relax the rule against expression statements beginning with {
for pattern assignments?
#2662
Comments
I think we could do that, and it's probably a source of confusion and irritation if we don't. Of course, we would probably rely on having a lexer that knows about matching up braces and other parenthesis-like tokens (in order to avoid an actual no-limits lookahead), so there might be a cost in terms of parsing with non-standard tools (Cider, Github, ...). |
Ah, this is a good catch. Yes, I think we should relax that restriction. There's no principled reason why map patterns shouldn't work in pattern assignments, and users will likely expect them to. |
Maybe a little contrived, because I'd be more likely to use a declaration pattern than an assignment pattern, but I'm sure it will happen, and it seems easy enough to support. It comes back to assuming that our parser can cheaply find the matching close-brace, which we do elsewhere too. Phrasing could be something like:
(We don't allow any other tokens surrounding a map pattern assignment, right? E.g., no |
Bug: #50035, dart-lang/language#2662 Change-Id: Ifb141cf563848fc0035423a625e27585cce2ea57 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/272523 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Jens Johansen <[email protected]>
Currently we have a disambiguation rule that says an expression statement can't begin with
{
, to prevent ambiguities between expression statements and blocks. This hasn't been a problem because the only way for an expression statement to start with{
was if it was a map literal followed by zero or more selectors, and in normal usage, that's always silly. For example:However, with the addition of pattern support, an expression statement starting with
{
could now be a pattern assignment where the pattern is a map pattern, e.g.:(Of course this is still a bit contrived; this would be better written as a pattern variable declaration, e.g.
var {'foo': foo, 'bar': bar} = x
. But I think the point still stands that a pattern assignment statement starting with{
is not so unreasonable.)I believe we could allow this code by changing the disambiguation rule from "an expression statement can't begin with
{
" to "an expression statement can only begin with{
if it's a patternAssignment". This won't introduce any crucial ambiguity, because a pattern assignment beginning with{
can always be told apart from a block by looking to see if the token to the right of the matching}
is=
.Should we?
The text was updated successfully, but these errors were encountered: