dart-lang / dart-lang/language

Allow parenthesized element expressions.

Open
#780 6 comments 2 reactions 0 assignees View on GitHub
request small-feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Currently list literal element expressions are either `if ...` or `for ...` or plain expressions. We do not allow parentheses around an `if ...` element, so the following:
```dart
var l = [ if (cond1) (if (cond2) 0) else if (cond3) 1 ];
```
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:
```dart
var l = [ if (cond1) if (cond2) 0 else ...[] else if (cond3) 1 ];
```

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:
```ebnf
::= `(' `)' | ...
```
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":
```dart
var l = [if (cond1) (value1, value2, value3)];
```
which is currently not something we can write shorter than:
```dart
var l = [if (cond1) ...[value1, value2, value3]];
```
The spread-literal does work, so it's not strictly necessary, but could be convenient. Effectively we would instead change the grammar to:
```ebnf
::= `(' `)' | ...
```
(where the `` 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).

Contributor guide

Open the contributing guide

Research direction

Start with the and grammar productions described in the issue, comparing parenthesized if elements with ordinary expressions and spreads. Decide whether the change covers only single parenthesized elements or also comma-separated values; done means the grammar specifies the accepted examples and resolves their ambiguity.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.