I would like to validate the argument to a function before its action is executed. The main concern is the passing of a void variable. How can I trap this?

(defun is-it-bound(item)
  (if (boundp 'item)
      (message "bound")
    (message "not bound")))

This doesn't work if I pass in a non-existent/undefined variable. Instead of getting "not bound", I get the "void-variable" explosion immediately. I want something like exception handling, so that I can trap the error.

I tried condition-case but the results are the same.

(defun is-it-bound(item)
  (condition-case nil
      (boundp 'item)
    (void-variable "%s" "void variable!" )

It looks like the argument is evaluated before being passed into the function. Is that correct?

In other languages, this is a no-brainer. But here, I need help getting it sorted. It's a brainer for me.


1 Answer 1


Yes, in Emacs Lisp, as in Common Lisp and most other Lisps, arguments are evaluated before evaluating the body of the function.

This is called "applicative" order evaluation. The opposite (passing the args unevaluated to the body, which evaluates them only as needed) is called "normal" order evaluation. (Well, there are actually multiple kinds of evaluation "as needed", but that's a detail here.)

Among other things, this means that you can end up with "wrong" (that is, perhaps unexpected) answers or infinite evaluation with Lisp. As one example, you can't define if as a function (you can define it as a macro, as Lisp macros don't evaluate their args before evaluating the body): each of the "consequence" args gets evaluated before the body of the function.

But I sense that you might have another question behind this one. That is, I suspect this is an X-Y question. Instead of trying to figure out how to do what you think you need to do, try asking how to do what you really want to do. ;-)

You say that your "main concern is the passing of a void variable." As you've seen, that's impossible - the function itself binds the arguments as local variables. Try to express your concern as a (separate) question.

Note that if what you want to do is "pass a variable", and not pass the variable's value, then you can do that in either of these ways. The first (use a function) is almost always preferable.

  1. Have callers of the function pass 'the-variable, not just the-variable to it. Quoting means that when the function is called it's passed the result of evaluating the quoted symbol, which is the symbol (the-variable). Note that the function can do whatever it wants with the symbol, including, but not limited to, treating it as a variable.

  2. Define a macro instead of a function. The macro body need not evaluate the variable passed to it as argument. (The macro body can do whatever else you want, to produce code that then gets evaluated.)

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.