]> git.eshelyaron.com Git - emacs.git/commit
New function 'hash-table-contains-p'
authorStefan Kangas <stefankangas@gmail.com>
Sat, 29 Mar 2025 13:59:26 +0000 (14:59 +0100)
committerEshel Yaron <me@eshelyaron.com>
Mon, 31 Mar 2025 08:37:50 +0000 (10:37 +0200)
commit273cc5055ea3c554d3249e834ea62f2808f65bf0
treed47b7304c7a36bfd4db90af4ecd1be79c42d0c91
parenta5a91aba96b3bf70cb3b03feb9117dc3daa5b54e
New function 'hash-table-contains-p'

This function tests whether a given key is present in a hash table.
Emacs Lisp has long lacked a standard way to do this, leading users to
write one of:

    (not (eq (gethash key table 'missing) 'missing))
or
    (gethash key table)

This idiom is error-prone (when 'missing' or 'nil' are valid values),
and it obscures intent.  The new function avoids such pitfalls,
improves readability, and makes the intent explicit:

    (hash-table-contains-p key table)

The name 'hash-table-contains-p' exists in other Lisp dialects (e.g.,
SRFI-125), and follows the precedent of 'seq-contains-p'.  Other
alternatives considered include `hash-table-has-key-p` and
`hash-table-key-exists-p`, but none were clearly better.

This was previously discussed in 2018, and all comments were positive,
but the proposed patch (implementing it in C) was never pushed:
https://lists.gnu.org/r/emacs-devel/2018-02/msg00424.html

* lisp/subr.el (hash-table-contains-p): New function.
* lisp/emacs-lisp/shortdoc.el (hash-table):
* doc/lispref/hash.texi (Other Hash): Document the new function.
* test/lisp/subr-tests.el (hash-table-contains-p): New test.

(cherry picked from commit dd0dd87e3aaf3116c400fba858cbe35ced15f04e)
doc/lispref/hash.texi
lisp/emacs-lisp/shortdoc.el
lisp/subr.el
test/lisp/subr-tests.el