@@ -112,7 +112,8 @@ The static type of `s` is `Set<T>`.
112112
113113#### Constant Set Literals
114114
115- If * s* starts with ` const ` , then it is a compile-time error if any element expression is not a compile-time constant expression,
115+ If * s* starts with ` const ` or it occurs in a constant context, then it is a * constant set literal* .
116+ It is then a compile-time error if any element expression is not a compile-time constant expression,
116117or if * T* is not a compile-time constant type. It is a compile-time error if any of the * values* of the constant element expressions
117118override ` Object.operator== ` unless they are instances of ` int ` or ` String ` , objects implementing ` Symbol ` originally created by
118119a symbol literal or a constant invocation of the ` Symbol ` constructor, or objects implementing ` Type ` originally created by
@@ -132,8 +133,8 @@ That is, constant set literals are canonicalized.
132133
133134#### Non-constant Set Literals
134135
135- If * s* does not start with ` const ` , then it evaluates to a mutable set object
136- as follows:
136+ If * s* does not start with ` const ` and it does not occur in a constant context,
137+ then it evaluates to a mutable set object as follows:
137138
138139Let * e<sub >1</sub >* … * e<sub >n</sub >* be the constant element expressions of * s* in source order.
139140Evaluation of * s* proceeds as follows:
@@ -287,32 +288,39 @@ since the new syntax is either not allowed by the existing grammar, or it is rej
287288
288289## Examples
289290``` dart
290- var v1 = {}; // Empty Map<dynamic, dynamic>
291- var v2 = <int, int>{}; // Empty Map<int, int>
292- var v3 = <int>{}; // Empty Set<int>
293- var v4 = {1: 1}; // Map<int, int>
294- var v5 = {1}; // Set<int>
295-
296- Iterable<int> v6 = {}; // Set<int>
297- Map<int, int> v7 = {}; // Map<int, int>
298- Object v8 = {}; // Map<dynamic, dynamic>
299- Iterable<num> v9 = {1}; // Set<num>
300- Iterable<num> v10 = <int>{}; // Set<int>
301- LinkedHashSet<int> v11 = {}; // Set<int>
291+ /*
292+ context expression runtime type and const-ness
293+ */
294+ var v1 = {}; // LinkedHashMap<dynamic, dynamic>
295+ var v2 = <int, int>{}; // LinkedMap<int, int>
296+ var v3 = <int>{}; // LinkedHashSet<int>
297+ var v4 = {1: 1}; // LinkedHashMap<int, int>
298+ var v5 = {1}; // LinkedHashSet<int>
299+
300+ Iterable<int> v6 = {}; // LinkedHashSet<int>
301+ Map<int, int> v7 = {}; // LinkedHashMap<int, int>
302+ Object v8 = {}; // LinkedHashMap<dynamic, dynamic>
303+ Iterable<num> v9 = {1}; // LinkedHashSet<num>
304+ Iterable<num> v10 = <int>{}; // LinkedHashSet<int>
305+ LinkedHashSet<int> v11 = {}; // LinkedHashSet<int>
302306
303307const v12 = {}; // const Map<dynamic, dynamic>
304308const v13 = {1}; // const Set<int>
305309const Set v14 = {} // const Set<dynamic>
310+ Set v15 = const {4} // const Set<dynamic>
306311
307312// Compile-time error, overrides `==`.
308- // const v15 = {Duration(seconds: 1)};
313+ // const _ = {Duration(seconds: 1)};
314+ // const _ = {2.3};
309315
310316var v16 = {1, 2, 3, 2, 1}; // Set<int>
311- var l16 = x.toList(); // <int>[1, 2, 3]
317+ var l16 = x.toList(); // -> <int>[1, 2, 3]
312318const v17 = {1, 2, 3, 2, 1}; // Set<int>
313- var l17 = x.toList(); // <int>[1, 2, 3]
319+ var l17 = x.toList(); // -> <int>[1, 2, 3]
314320// v17.add(42); // throws, immutable
321+ var l18 = const {1, 2} // const Set<int>
315322
323+ // Class overriding `==`.
316324class C {
317325 final int id;
318326 final String name;
@@ -323,14 +331,19 @@ class C {
323331}
324332
325333// First equal object wins.
326- var v18 = {C(1, "a"), C(2, "a"), C("1", b")}; // Set<C>
327- print(v18); // {C(1, "a"), C(2, "a")}
328-
329- const v19 = {1, 2, 3};
330- const v20 = {3, 2, 1};
331- const v21 = {1, 1, 2, 3, 2, 1};
332- print(identical(v19, v20)); // false
333- print(identical(v19, v21)); // true
334+ var v19 = {C(1, "a"), C(2, "a"), C("1", b")}; // LinkedHashSet<C>
335+ print(v19); // {C(1, "a"), C(2, "a")}
336+
337+ const v20 = {1, 2, 3}; // const Set<int>
338+ const v21 = {3, 2, 1}; // const Set<int>
339+ const v22 = {1, 1, 2, 3, 2, 1}; // const Set<int>
340+ print(identical(v20, v21)); // -> false
341+ print(identical(v20, v22)); // -> true
342+
343+ // Type can be computed from element types.
344+ var v23 = {1, 2.5} // LinkedHashSet<num>
345+ var v24 = {1, false} // LinkedHashSet<Object>
346+ const v26 = {1, false} // const Set<Object>
334347```
335348
336349### With Spreads
0 commit comments