* Handling nil values in optional argument
@ 2024-07-11 22:38 Heime
2024-07-12 2:37 ` [External] : " Drew Adams
0 siblings, 1 reply; 5+ messages in thread
From: Heime @ 2024-07-11 22:38 UTC (permalink / raw)
To: Heime via Users list for the GNU Emacs text editor
I have the following tool, but I want that if I pass nil in the third argument,
the function does not assume that fyr was not supplied, but that fyr is actually
to be used as a nil value.
(defun mundu (monbf funcs labels &optional fyr)
(unless fyr (setq fyr t))
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: [External] : Handling nil values in optional argument
2024-07-11 22:38 Handling nil values in optional argument Heime
@ 2024-07-12 2:37 ` Drew Adams
2024-07-12 4:37 ` Michael Heerdegen via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 5+ messages in thread
From: Drew Adams @ 2024-07-12 2:37 UTC (permalink / raw)
To: Heime, Heime via Users list for the GNU Emacs text editor
> I have the following tool, but I want that if I pass nil in the third
> argument,
> the function does not assume that fyr was not supplied, but that fyr is
> actually
> to be used as a nil value.
>
> (defun mundu (monbf funcs labels &optional fyr)
> (unless fyr (setq fyr t))
You can't. nil is the default value for an optional argument.
Instead, your code can use something other than nil for the value that you pass as an explicit argument. It can use the singleton list (nil), for example.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [External] : Handling nil values in optional argument
2024-07-12 2:37 ` [External] : " Drew Adams
@ 2024-07-12 4:37 ` Michael Heerdegen via Users list for the GNU Emacs text editor
2024-07-12 12:48 ` Heime
0 siblings, 1 reply; 5+ messages in thread
From: Michael Heerdegen via Users list for the GNU Emacs text editor @ 2024-07-12 4:37 UTC (permalink / raw)
To: help-gnu-emacs
Drew Adams <drew.adams@oracle.com> writes:
> Instead, your code can use something other than nil for the value that
> you pass as an explicit argument. It can use the singleton list
> (nil), for example.
Alternatively one can give the defun an &rest argument list and look at
the length of the specified list in the body. The argument variables
would have to be assigned in the body. Use an advertised calling
convention to get argument name support back for eldoc etc.
I guess that's more or less what's cl-defun also does. So one can also
use cl-defun. Or just do as Drew said.
Michael.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [External] : Handling nil values in optional argument
2024-07-12 4:37 ` Michael Heerdegen via Users list for the GNU Emacs text editor
@ 2024-07-12 12:48 ` Heime
2024-07-13 4:56 ` Michael Heerdegen via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 5+ messages in thread
From: Heime @ 2024-07-12 12:48 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
On Friday, July 12th, 2024 at 4:37 PM, Michael Heerdegen via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> Drew Adams drew.adams@oracle.com writes:
>
> > Instead, your code can use something other than nil for the value that
> > you pass as an explicit argument. It can use the singleton list
> > (nil), for example.
>
>
> Alternatively one can give the defun an &rest argument list and look at
> the length of the specified list in the body. The argument variables
> would have to be assigned in the body. Use an advertised calling
> convention to get argument name support back for eldoc etc.
>
> I guess that's more or less what's cl-defun also does. So one can also
> use cl-defun. Or just do as Drew said.
>
>
> Michael.
Can you show an example of using &rest for the specified purpose.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [External] : Handling nil values in optional argument
2024-07-12 12:48 ` Heime
@ 2024-07-13 4:56 ` Michael Heerdegen via Users list for the GNU Emacs text editor
0 siblings, 0 replies; 5+ messages in thread
From: Michael Heerdegen via Users list for the GNU Emacs text editor @ 2024-07-13 4:56 UTC (permalink / raw)
To: help-gnu-emacs
Heime <heimeborgia@protonmail.com> writes:
> Can you show an example of using &rest for the specified purpose.
Here's a template of a function accepting two and optionally a third
argument:
#+begin_src emacs-lisp
(defun my-function (a b &rest r)
(declare (advertised-calling-convention
(a b &optional c) nil))
(when (cdr r)
(signal 'wrong-number-of-arguments
(list '(2 . 3) (+ 2 (length r)))))
(let ((c (car r)))
;; put the function body here...
(message "a: %S; b: %S; c: %s"
a b (if r (prin1-to-string c) "not specified"))))
#+end_src
Michael.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-07-13 4:56 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-11 22:38 Handling nil values in optional argument Heime
2024-07-12 2:37 ` [External] : " Drew Adams
2024-07-12 4:37 ` Michael Heerdegen via Users list for the GNU Emacs text editor
2024-07-12 12:48 ` Heime
2024-07-13 4:56 ` Michael Heerdegen via Users list for the GNU Emacs text editor
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.