Should entry insertion and entry modification be distinct operations from upsertion?
Daphne Preston-Kendal 29 Jul 2025 09:33 UTC
hash-table-set! is an upsert operation which actually does two distinct things:
1. if a key already has an entry associated with it, the value associated with that key is modified
2. otherwise, a new entry is created associating the given key with the given value
I think these should (also) be provided as distinct operations. I will call them hash-table-add! and hash-table-replace! but these could be bikeshedded:
1. hash-table-add! creates a new entry and signals an assertion violation if the key already exists
2. hash-table-replace! updates an existing entry and signals an assertion violation if the key does not already exist
This would help avoiding accidentally clobbering existing entries when building up a hash table one by one, and avoid accidentally inserting new entries when intending to change values of (many) keys. hash-table-set! as an upsert operation would remain.
What do people think?
Daphne