0

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 bof 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".

4
  • 1
    I think you are confusing yourself: (or b t) will evaluate to the value of b if it is not nil and it will evaluate to t if it the value of b is nil. Isn't that exactly what you want? Try (let ((b nil)) (or b t)) and (let ((b '(a b c))) (or b t)).
    – NickD
    Commented Jun 2, 2023 at 1:38
  • emacs.stackexchange.com/tags/elisp/info
    – NickD
    Commented Jun 2, 2023 at 1:45
  • @NickD Thanks. Just fixed the typo.
    – tinlyx
    Commented Jun 2, 2023 at 3:40
  • So the problem is that if the optional parameter is absent, it is interpreted within the function as nil, which is indistinguishable from the parameter being present with value nil - but you would like to treat these two cases differently: in the first case, you would like to interpret it as being t - yes?
    – NickD
    Commented Jun 2, 2023 at 4:01

2 Answers 2

1

What does it even mean to you, to default to t? Defaulting for a parameter means that if the parameter isn't present explicitly (it's absent) then it gets the default value.

For Lisp parameters, absence is the same presence as nil - that's just the way it is. You (your code) can't distinguish the presence of a nil value from the absence of any value.

So if you write (or nil t) then whether the parameter is present as nil or is absent, the value you get will be t. Is that the behavior you want?

This has the scent of an X-Y problem. What is it that you're really trying to do?

3
  • Thanks for the answer. I have added the motivation why I needed a default-true parameter.
    – tinlyx
    Commented Jun 2, 2023 at 3:37
  • What you really want - still isn't clear. You say you want a command, but you're defining a non-command, a noninteractive function. I'm guessing maybe you want some defaulting to t interactively but not when the function is called from Lisp? If so, you can do that with an interactive form. But you'd have to say more about how the arguments (e.g. a) would be provided interactively (e.g., prompted for?). Overall, the question seems unclear or misguided. You'll likely get a helpful answer from someone if you figure out how to specify your (real) question clearly. That's my guess.
    – Drew
    Commented Jun 2, 2023 at 4:16
  • For completeness, I wanted to write a load function that loads a user .el file only when a pre-condition holds, which is the b here. But the more general question is language-wise: how to default to true. Hence the question here.
    – tinlyx
    Commented Jun 2, 2023 at 8:10
0

If my interpretation of your question is correct:

So the problem is that if the optional parameter is absent, it is interpreted within the function as nil, which is indistinguishable from the parameter being present with value nil - but you would like to treat these two cases differently: in the first case, you would like to interpret it as being t - yes?

the answer is: you cannot do that. See Features of Argument Lists in the Emacs Lisp reference manual, where it states:

If actual arguments for the optional and rest variables are omitted, then they always default to ‘nil’. There is no way for the function to distinguish between an explicit argument of ‘nil’ and an omitted argument. However, the body of the function is free to consider ‘nil’ an abbreviation for some other meaningful value. This is what ‘substring’ does; ‘nil’ as the third argument to ‘substring’ means to use the length of the string supplied.

Common Lisp note: Common Lisp allows the function to specify what
default value to use when an optional argument is omitted; Emacs
Lisp always uses ‘nil’.  Emacs Lisp does not support ‘supplied-p’
variables that tell you whether an argument was explicitly passed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.