# RFC: hash table pattern matching

**URL:** <https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686>\
**Category:** General\
**Created:** [February 3, 2023, 10:00pm UTC](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686 "2023-02-03T22:00:51Z")\
**Posts on this page:** 9\
**Page:** 4

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [February 24, 2023, 7:25pm UTC](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686/61 "2023-02-24T19:25:36Z")

</div>

A few thoughts on this:

1. We should look at actual uses of the `hash-table` pattern across real Racket code to see whether people are looking for partial or full matching.
2. What do other languages do here? There's a proposal for JavaScript, for example, here: [GitHub - tc39/proposal-pattern-matching: Pattern matching syntax for ECMAScript](https://github.com/tc39/proposal-pattern-matching#motivating-examples) In that, it uses partial matching and also allows a rest pattern for the other keys.

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [February 24, 2023, 7:33pm UTC](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686/62 "2023-02-24T19:33:18Z")

</div>

> [@sorawee](#):
>
> > Maybe it would be useful to articulate why we’re discussing adding a pattern with special support from match, rather than a pure library extension? (That is what we’re discussing, right?) I think it’s a combination of (a) bugs in hash-table and (b) potential efficiency gains from cooperation with match’s optimizer, but maybe there are other reasons.
> 
> It’s mainly because of the bugs in the existing `hash-table`. I need to add something so that users can switch over and stop using the buggy pattern. `hash-table` is already expressible as a match expander. Same for the proposed `hash` and `hash*`. There’s no efficiency gain.

Isn't there?

Here's some pseudo-code based on the [Activity Vocabulary](https://www.w3.org/TR/activitystreams-vocabulary/):

```scheme
(match-lambda
  [(hash '@context "https://www.w3.org/ns/activitystreams"
         'type "Like"
         'summary desc
         'actor who
         'object what)
   ...]
  [(hash '@context "https://www.w3.org/ns/activitystreams"
         'type "Question"
         'name question
         (and (or 'oneOf 'anyOf) mode) options)
   ...]
  [(hash '@context "https://www.w3.org/ns/activitystreams"
         'type "Block"
         'summary desc
         'actor who
         'object what)
   ...])

```

I see lots of opportunities for a sufficiently smart `match` to optimize this, but I don't think `match` can do them with just `?` and `app` without trying doing an unreasonable amount of analysis to the procedure expressions.

(Writing this also made me think about how the potential choices might interact with `and` and `or` patterns.)

> [@AlexKnauth](#):
>
> This is also why I don't really like the words "partial" and "full". In the same message where @rocketnia pointed this out about structs, there were other naming suggestions floated: `#:extensible` or `#:open` instead of `#:partial`, and `#:final` or `#:closed` instead of `#:full`.

I also don't love the words "full" and "partial". I've been thinking of the word "exhaustive", but I'm not sure if the analogy to "exhaustiveness checking" would be helpful or confusing.

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [February 28, 2023, 1:42am UTC](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686/63 "2023-02-28T01:42:20Z")

</div>

I like “open” and “closed” better, so I will use those.

It’s been a week now. The results of both polls are: 9 vs 5 and 7 vs 3 for “closed” vs “open”. So the consensus seems to be having “closed” by default for `hash`.

I think I already have enough information from the RFC to start implementing the pattern. Of course, if you have any disagreement with the upcoming PR, you can still raise your concern there.

@LiberalArtist: When I said there’s no efficiency gain, I simply described the current state (of the existing `hash-table` and the proposed `hash` and `hash*`). With more effort, you can certainly add more optimization.

The second pattern in your code snippet is interesting. It is not possible to directly express it in the proposed patterns. However, you can do the following (assuming that options can't be false):

```scheme
  [(hash* ['@context "[https://www.w3.org/ns/activitystreams](https://www.w3.org/ns/activitystreams)"]
          ['type "Question"]
          ['name question]
          ['oneOf options-one #:default #f]
          ['anyOf options-any #:default #f])
   #:when (or options-one options-any)
   (define options (or options-one options-any))
   (define mode (if options-one 'oneOf 'anyOf))
   ...]

```

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [February 28, 2023, 3:20am UTC](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686/64 "2023-02-28T03:20:17Z")

</div>

Oh, and thank you to everyone for participating!

---

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [October 25, 2023, 3:36am UTC](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686/65 "2023-10-25T03:36:39Z")

</div>

I circled back on this in the process of looking something else up and was wondering where things stood? I use match an awful lot, and this particular feature would be very useful for me.

---

<div class="post-metadata">

**Author:** ![shhyou](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shhyou](https://racket.discourse.group/u/shhyou)\
**Post date:** [October 25, 2023, 4:31am UTC](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686/66 "2023-10-25T04:31:49Z")

</div>

The PR is sort of stuck.

> <https://github.com/racket/racket/pull/4632>
>
> As discussed in
> https://racket.discourse.group/t/rfc-hash-table-pattern-matchin…g/1686 and https://github.com/racket/racket/pull/4532
> the currrent hash-table is buggy, so we would like to deprecate it and replace with a new set of patterns that are well-behaved.

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [October 25, 2023, 5:06am UTC](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686/67 "2023-10-25T05:06:55Z")

</div>

I plan to resolve this issue with @samth at RacketCon.

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [January 2, 2024, 2:54pm UTC](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686/68 "2024-01-02T14:54:36Z")

</div>

The PR is now merged! The feature is in the [snapshot build](https://users.cs.utah.edu/plt/snapshots/current/) now ([docs here](https://users.cs.utah.edu/plt/snapshots/current/doc/reference/match.html)), so you can try it out if you want to.  
Thanks everyone in this thread for the RFC participation. Special thanks to @samth for the PR review!

---

<div class="post-metadata">

**Author:** ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)\
**Post date:** [January 10, 2024, 12:24pm UTC](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686/69 "2024-01-10T12:24:54Z")

</div>

Net betyds vir my verjaarsdag!

Thanks, @sorawee (and to whomever thanks are in order). I've been eyeing this feature for a while now.

[Previous page](https://racket.discourse.group/t/rfc-hash-table-pattern-matching/1686.md?page=3)
