In the question Optional parameter defaults, the suggested way to set default value for the parameter of an elisp function is along the lines of:
(defun my-command (a &optional b)
(command-body a (or b default)))
Here, the optional parameter b
of my-command
is replaced by (or b default)
.
I had a hard time trying to use this method to default b
to true. I used (or b t)
. But this value will be always a true, regardless of whether b
is nil
(the logical false) or not.
How can one default the elisp optional parameter to true?
-- Edit --
To clarify the question: What does it even mean to you, to default to t?
I am trying to write a function that runs only when a per-condition is met. This per-condition is represented as an optional parameter like b
here that defaults to true.
The problem seems to be that nil
is used both as the logical false and as "absence of value".
(or b t)
will evaluate to the value ofb
if it is notnil
and it will evaluate tot
if it the value ofb
isnil
. Isn't that exactly what you want? Try(let ((b nil)) (or b t))
and(let ((b '(a b c))) (or b t))
.nil
- but you would like to treat these two cases differently: in the first case, you would like to interpret it as beingt
- yes?