Skip to content

Commit d3a2ad4

Browse files
munificenteernstg
andauthored
[patterns] Clarify when map key primitive == comes into play. (#2691)
* [patterns] Clarify when map key primitive `==` comes into play. Fix #2690. * Update accepted/future-releases/0546-patterns/feature-specification.md Co-authored-by: Erik Ernst <[email protected]> Co-authored-by: Erik Ernst <[email protected]>
1 parent c87d200 commit d3a2ad4

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

accepted/future-releases/0546-patterns/feature-specification.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -595,27 +595,29 @@ It is a compile-time error if:
595595

596596
* Any of the entry key expressions are not constant expressions.
597597

598-
* If any two keys in the map are identical. *Map patterns that don't have a
599-
rest element only match if the `length` of the map is equal to the number of
600-
map entries. If a map pattern has multiple identical key entries, they will
598+
* Any two keys in the map are identical. *Map patterns that don't have a rest
599+
element only match if the `length` of the map is equal to the number of map
600+
entries. If a map pattern has multiple identical key entries, they will
601601
increase the required length for the pattern to match but in all but the
602602
most perverse `Map` implementations will represent the same key. Thus, it's
603603
very unlikely that any map pattern containing identical keys (and no rest
604604
element) will ever match. Duplicate keys are most likely a typo in the
605605
code.*
606606

607-
* If any two keys in the map both have primitive `==` methods, then it is a
608-
compile-time error if they are equal according to their `==` operator. *In
609-
cases where keys have types whose equality can be checked at compile time,
610-
we report errors if there are redundant keys. But we don't require the keys
611-
to have primitive equality for flexibility. In map patterns where the keys
612-
don't have primitive equality, it is possible to have redundant keys and the
613-
compiler won't detect it.*
607+
* Any two record keys which both have primitive `==` are equal. *Since
608+
records don't have defined identity, we can't use the previous rule to
609+
detect identical records. But records do support an equality test known at
610+
compile time if all of their fields do, so we use that.*
614611

615612
* There is more than one `...` element in the map pattern.
616613

617614
* The `...` element is not the last element in the map pattern.
618615

616+
*Note that we don't require map keys to have primitive `==` methods to enable
617+
more flexibility in key types. If the keys have user-defined `==` methods, then
618+
it's possible to have keys that are equal according to those `==` methods, but
619+
the compiler won't detect it.*
620+
619621
### Rest elements
620622

621623
Like lists, map patterns can also have a rest element. However, there's no
@@ -3221,6 +3223,8 @@ Here is one way it could be broken down into separate pieces:
32213223
32223224
- Clarify which variables are valid in pattern assignments.
32233225
3226+
- Clarify when primitive `==` for map pattern keys comes into play (#2690).
3227+
32243228
### 2.19
32253229
32263230
- Specify exhaustiveness checking of switch elements.

0 commit comments

Comments
 (0)