0

If I have an optional function argument, how can I determine if name has been supplied. And if it has been supplied, how can I test its validity (not nil or not empty string).

    (defun myfunc (&optional name)
      "DOCSTRING"
      (if name
          (do-this)
        (do-that)))

Does the if statement only check for name being nil?

Drew
  • 75,699
  • 9
  • 109
  • 225
Dilna
  • 1,173
  • 3
  • 10

2 Answers2

2

Does the if statement only check for [its first arg] being nil?

Yes.

And you can just ask Emacs: C-h f if.

if is a special form in C source code.

(if COND THEN ELSE...)

If COND yields non-nil, do THEN, else do ELSE...

Returns the value of THEN or the value of the last of the ELSE's.

THEN must be one expression, but ELSE... can be zero or more expressions.

If COND yields nil, and there are no ELSE's, the value is nil.

Drew
  • 75,699
  • 9
  • 109
  • 225
0

if switches on nil or non-nil so something like

(if (and name (not string-empty-p name))
    (do-this)
  (do-that))

will do the job.

Fran Burstall
  • 3,665
  • 10
  • 18