I have a function argument and want to set an empty string for name
and mode
. What can I do?
(defun bench (&optional name mode prefix)
"DOCSTRING"
(interactive
(cond
((equal current-prefix-arg nil)
(list
current-prefix-arg)))))
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.
(if name
and (if mode
or (if (string= "" name)
.
– Dilna
Jul 09 '22 at 13:56
nil
or ""
when using the function interactively.
– Dilna
Jul 09 '22 at 13:59
cl-defun
allows you to set defaults for optional arguments. DoC-h f cl-defun
to learn more. – Fran Burstall Jul 09 '22 at 07:13(list "" "" current-perfix-arg)
? – Dilna Jul 09 '22 at 07:30