-
Notifications
You must be signed in to change notification settings - Fork 207
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
Allow parenthesized element expressions. #780
Comments
That's an interesting edge case I hadn't considered. I think we could probably allow this.
The other, simpler, option is: var l = [ if (cond1) ...[if (cond2) 0] else if (cond3) 1 ]; Basically, |
It should just work: the following rule can be used in Dart.g without breaking anything in element
: expressionElement
| mapEntry
| spreadElement
| ifElement
| forElement
| '(' element ')'
; |
Good to know. I'm also slightly worried about it preventing other future syntax changes. For example, I could imagine wanting to support |
Why not: var l = [ if (cond1) { if (cond2) 0 } else if (cond3) 1 ]; Because Parentheses are usually used for grouping expressions and braces for statements. "Collection elements" feels closer to expressions than statements - they introduce something into the place where they occur. |
Wrong? We like braces for grouping statements. We like parentheses for grouping sub-expressions. We don't generally group multiple expressions, those mainly exist lists (parameter lists, collection literals), and they are comma separated instead and the braces are delimiters, not grouping/precedence-operators. The problem we're trying to solve here is precedence among collection elements. If the goal was to allow multiple elements inside an I know it's all "look and feel" arguments, so clearly subjective. 😞 (And, whoops, interpolations use |
I agree that some Unix shell (or Perl, I think awk used C# also uses braces, just without the |
Currently list literal element expressions are either
if ...
orfor ...
or plain expressions. We do not allow parentheses around anif ...
element, so the following:is not allowed. That is inconvenient because we also do not have a "no element" element that is briefer than
...[]
, so the way to write the above logic today is:We should consider allowing parentheses around all element expressions. The only place it actually matters is delimiting an
if
with no else, but that is an annoying surprise in the current grammar.The change would be adding the following
elements
production:which is ambiguous with parenthesized expressions, but since any ambiguous element/expression means the same thing whether it's an element or an expression, it doesn't matter how we resolve the ambiguity.
Optionally, also allow multiple values, comma separated, inside an "elements parenthesis":
which is currently not something we can write shorter than:
The spread-literal does work, so it's not strictly necessary, but could be convenient. Effectively we would instead change the grammar to:
(where the
<elements>
production allows trailing commas).That grammar is no more ambiguous than the one above, any presence of a comma will definitely disambiguate towards the element production.
(Edit: Added multiple-values option and grammar).
The text was updated successfully, but these errors were encountered: