2

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
Tianxiang Xiong
  • 3,848
  • 16
  • 27
  • 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 '15 at 02: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. – Tianxiang Xiong Dec 28 '15 at 03: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 '15 at 10:59

0 Answers0