Skip to content

Commit d45fac1

Browse files
authored
Update feature-specification.md
1 parent cccc6ed commit d45fac1

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

accepted/future-releases/set-literals/feature-specification.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,23 @@ foo({1}, {});
268268

269269
In any case, this needs to be finalized if/when we introduce literal spreads.
270270

271+
## Summary
272+
Set literals use `{` and `}` as delimiters, allows a single type argument, and has comma-separated expressions for elements.
273+
274+
This syntax is distinct from map literal syntax (two type arguments, colon-pairs as elements) except for the no-type-argument empty literal `{}`.
275+
In that case, we make it a set if the context type allows a set, and it does not allow a map, otherwise we make it a map.
276+
277+
Type inference works just as for list literals,
278+
and the literal has an "exact" type (`LinkedHashSet<E>` for non-const sets,
279+
`Set<E>` for const sets, similar to map literals).
280+
281+
The meaning of a set `<E>{e1, ..., en}` is a set with the same elements and iteration order as `new Set<E>()..add(e1) ... ..add(en)`.
282+
283+
Const set literals are allowed. Elements need to satisfy the same requirements as constant map keys (not overriding `Object.==` unless it's an integer, string or `Symbol`).
284+
285+
Adding set literals like this will not change any existing compilable program,
286+
since the new syntax is either not allowed by the existing grammar, or it is rejected by the static type system.
287+
271288
## Examples
272289
```dart
273290
var v1 = {}; // Empty Map<dynamic, dynamic>
@@ -276,7 +293,7 @@ var v3 = <int>{}; // Empty Set<int>
276293
var v4 = {1: 1}; // Map<int, int>
277294
var v5 = {1}; // Set<int>
278295
279-
Iterable<int> v6 = {}; // Set<int>
296+
Iterable<int> v6 = {}; in // Set<int>
280297
Map<int, int> v7 = {}; // Map<int, int>
281298
Object v8 = {}; // Map<dynamic, dynamic>
282299
Iterable<num> v9 = {1}; // Set<num>
@@ -289,7 +306,7 @@ const Set v14 = {} // const Set<dynamic>
289306
290307
// Compile-time error, overrides `==`.
291308
// const v15 = {Duration(seconds: 1)};
292-
```
309+
293310
var v16 = {1, 2, 3, 2, 1}; // Set<int>
294311
var l16 = x.toList(); // <int>[1, 2, 3]
295312
const v17 = {1, 2, 3, 2, 1}; // Set<int>

0 commit comments

Comments
 (0)