help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

(interactive) and &optional


From: Dr Rainer Woitok
Subject: (interactive) and &optional
Date: Thu, 23 Mar 2023 20:10:32 +0100

Greetings,

In an attempt  to write a function  with and optional argument  which is
both,  callable from Lisp and via "M-x",  I ran into some unexpected (by
me) problems.  Consider the following function:

   (defun fun (&optional arg)
   (interactive "Sarg: ")
   (message "%s:%s.\n" 'val (symbol-name arg)))

Calling "M-: (fun 'a)" returns

   "val:a.
   "

including the double quotes,  while calling "M-x fun" and then typing "a
RET" at the prompt returns

   val:a.\n

without double quotes.  Apart from perhaps the double quotes, this is
what I had expected.  Likewise, calling "M-: (fun)" returns

  "val:nil.
  "

as expected,  while calling "M-x fun" and then just typing  "RET" at the
prompt returns

  val:.\n

that is, an empty symbol or string.

Am I really expected in a function that is both,  callable from Lisp and
via "M-x", to code something along the lines of

   (cond ((or (null arg) (string-empty-p arg)) 'default-val)
         (arg))

to check whether or not an optional argument has been passed?  Are there
any more elegant ways to achieve this?

Any pointers welcome :-)

Sincerely,
  Rainer



reply via email to

[Prev in Thread] Current Thread [Next in Thread]