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?

2 Answers 2

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.

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.

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.