0

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)))))

Dilna
  • 1
  • 3
  • 11

1 Answers1

1

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.

db48x
  • 17,977
  • 1
  • 22
  • 28
  • In my function I was only doing (if name and (if mode or (if (string= "" name). – Dilna Jul 09 '22 at 13:56
  • But I still have to set three values for the list that fills the three function arguments. Would I use nil or "" when using the function interactively. – Dilna Jul 09 '22 at 13:59
  • No, you don’t have to. But if you want to use that type of interactive form, you can use whatever values you want in the list. – db48x Jul 09 '22 at 15:19
  • If I want to use that type of interactive form, then I have to make the three valued list, right? If I have optional arguments, can I just fill in just some of them in the interactive list setup? – Dilna Jul 09 '22 at 17:19
  • 1
    With this type of interactive form, you must supply a list of values to be used as arguments. They will be applied to the arguments in precisely the same manner as any other list of arguments. This means that the values are applied to the arguments in order, and you do not have to specify a value for the optional arguments. There is really nothing complex here. Did you try something that failed? If so, you really should include it, and the error you got, in the question. Otherwise people will think you haven’t even bothered to try it. – db48x Jul 09 '22 at 18:32