After taking a look at the definition of defun in byte-run.el, it seems that the macro does some very interesting things regarding checking for the existence of a docstring.

First, it uses a declare form to indicate that the third argument to the defun is the docstring. It then checks whether the third argument is a string (which means it's a docstring), or actually a declare form (which means there is no docstring).

If I understand correctly, this allows the user of a defun to specify a docstring or to it out altogether.

Is this use of an optional argument considered unusual? Usually, for a function (foo a b &optional c d &rest rest), we'd enter a nil for c if we wanted to specify d but not c. I haven't seen other examples of arguments that can simply be "left out" like the docstring.

  • 1
    defun is not a function. As you said, it is a macro. And yes, its treatment is special in the way you described. Is that really the question - whether defun is handled in a different way from what is usual for functions? If not, maybe try to clarify the question.
    – Drew
    Dec 28, 2015 at 2:05
  • Yes, that is the question. It's out of curiosity as to what is idiomatic in Emacs Lisp. And I don't really understand the issue of defun as a macro vs. function on this topic. As I understand it, this behavior (of being able to leave out a parameter) is uncommon in both. Dec 28, 2015 at 3:32
  • 1
    Then the answer is yes. A macro typically does not evaluate its arguments. Instead, it uses its arguments to construct a sexp, which is then evaluated. Because a macro does not evaluate its args, it can do anything it likes with them, including ignore them. You can define if as a macro, for this reason. You cannot define if as a function, because all of its args would be evaluated before the function body is evaluated. Likewise, defun could not be a function and get away with testing whether an unevaluated arg is a literal string, etc. (You should read up on macros in the doc.)
    – Drew
    Dec 28, 2015 at 10:59


Your Answer

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

Browse other questions tagged or ask your own question.