1

I'm using emacs 24.3 right now, so hash-table-values is not available. So I want to write the function, but only if it doesn't exist. This way, my code works right now, and it'll use the default function when I'll switch to emacs 24.4.

In PHP, I'd write something like:

if (!function_exists('hash_table_values')) {
    function hash_table_values() {}
}

Is there some equivalent in elisp?

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
  • 1
    Note that even in 24.4, `hash-table-values` is only available if you explicitly require `subr-x`. – legoscia Jul 02 '14 at 09:32

1 Answers1

2

Thanks to some guidance on #emacs@freenode, here is the magic function: fboundp.

(unless (fboundp 'fn)
  (defun fn ()))

For the real implementation of hash-table-values:

(unless (fboundp 'hash-table-values)
  (defun hash-table-values (hashtable)
    (let (allvals)
      (maphash (lambda (_kk vv) (push vv allvals)) hashtable)
    allvals)))

Thanks to ergoemacs for the hash-table-values implementation.

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
  • `(setq x (cons v x)` can be shortened to `(push v x)`. Also, adding a leading underscore to the unused name `kk` avoids a warning about an unused lexical variable, in case lexical binding is enabled. –  Jul 02 '14 at 09:27