I don’t really know what you want, but based on your comment you want the name
and mode
variables to be strings if the caller passes in nil or omits them. While cl-defun
is an option, I wouldn’t do anything fancy at all.
Instead, when you go to use name
or mode
in the body of your function, just write (or name "")
and (or mode "")
instead. The or
function looks at each argument in turn and gives you the first one that is non–nil. Like this:
(defun bench (&optional name mode prefix)
"DOCSTRING"
(message "name=%s mode=%s" (or name "") (or mode ""))
If you find yourself in a situation where you need to type these expressions out more than once, just use let
to rebind them:
(defun bench (&optional name mode prefix)
"DOCSTRING"
(let ((name (or name ""))
(mode (or mode "")))
…))
I wouldn’t try to do anything tricky with the interactive
declaration. Keep it as simple as possible.
cl-defun
allows you to set defaults for optional arguments. DoC-h f cl-defun
to learn more.(list "" "" current-perfix-arg)
?