unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* (interactive) and &optional
@ 2023-03-23 19:10 Dr Rainer Woitok
  2023-03-23 19:20 ` Philip Kaludercic
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Dr Rainer Woitok @ 2023-03-23 19:10 UTC (permalink / raw)
  To: Help-Gnu-Emacs

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



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2023-03-30 19:04 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-23 19:10 (interactive) and &optional Dr Rainer Woitok
2023-03-23 19:20 ` Philip Kaludercic
2023-03-25 12:35   ` Dr Rainer Woitok
2023-03-25 13:12     ` Philip Kaludercic
2023-03-25 14:33       ` [External] : " Drew Adams
2023-03-25 15:36         ` Dr Rainer Woitok
2023-03-25 16:58         ` Philip Kaludercic
2023-03-23 20:01 ` Emanuel Berg
2023-03-24  9:23 ` Jean Louis
2023-03-24 20:52   ` Philip Kaludercic
2023-03-24 21:07     ` Emanuel Berg
2023-03-27  1:52       ` [External] : " Drew Adams
2023-03-27  2:40         ` John Yates
2023-03-24 21:42     ` Jean Louis
2023-03-26  1:04       ` Emanuel Berg
2023-03-30 19:04         ` Jean Louis
2023-03-25 12:05   ` Dr Rainer Woitok
2023-03-25 14:32     ` Jean Louis
2023-03-25 15:41       ` Dr Rainer Woitok

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).