emacselispfuture-proof

Future-proof defun - use if available, define equivalent if not


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?


Solution

  • 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.