Skip to content

Commit 36a2a98

Browse files
authored
Disallow equal elements in const sets.
State that it is a compile-time error to have equal entries in a const set literal. That is what maps do for keys.
1 parent 5956460 commit 36a2a98

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ or if *T* is not a compile-time constant type. It is a compile-time error if any
118118
override `Object.operator==` unless they are instances of `int` or `String`, objects implementing `Symbol` originally created by
119119
a symbol literal or a constant invocation of the `Symbol` constructor, or objects implementing `Type` originally created by
120120
a constant type literal expression.
121+
It is a compile-time error if any two of the values are equal according to `==`.
121122

122123
Let *e<sub>1</sub>**e<sub>n</sub>* be the constant element expressions of *s* in source order,
123124
and let *v*<sub>1</sub> … *v<sub>n</sub>* be their respective constant values.
@@ -315,9 +316,9 @@ Set v15 = const {4} // const Set<dynamic>
315316
316317
var v16 = {1, 2, 3, 2, 1}; // LinkedHashSet<int>
317318
var l16 = x.toList(); // -> <int>[1, 2, 3]
318-
const v17 = {1, 2, 3, 2, 1}; // const Set<int>
319-
var l17 = x.toList(); // -> <int>[1, 2, 3]
320-
// v17.add(42); // throws, immutable
319+
// Compile-time error, contains equal elements
320+
// const _ = {1, 2, 3, 2, 1};
321+
321322
var l18 = const {1, 2} // const Set<int>
322323
323324
// Class overriding `==`.
@@ -336,9 +337,7 @@ print(v19); // {C(1, "a"), C(2, "a")}
336337
337338
const v20 = {1, 2, 3}; // const Set<int>
338339
const v21 = {3, 2, 1}; // const Set<int>
339-
const v22 = {1, 1, 2, 3, 2, 1}; // const Set<int>
340340
print(identical(v20, v21)); // -> false
341-
print(identical(v20, v22)); // -> true
342341
343342
// Type can be computed from element types.
344343
var v23 = {1, 2.5} // LinkedHashSet<num>

0 commit comments

Comments
 (0)