When should Emacs #'function syntax be used?

前端 未结 3 392
梦谈多话
梦谈多话 2020-11-30 03:16

Basically, when should I use Emacs Lisp\'s function procedure? I haven\'t found any examples in which there\'s a difference in behavior if you pass functions as

3条回答
  •  -上瘾入骨i
    2020-11-30 03:40

    In addition to the differences when quoting (lambda ...) forms there has recently (as of Emacs 24.4) been a change to bytecomp.el such that a warning is generated when a #'symbol form is used, but the function is not known to get defined at the end of the compilation.

    Writing all function symbols with the function-quoting #'symbol syntax rather than the 'symbol syntax is thus somewhat preferable, since it allows the byte-compiler to check if you have used function names that are actually defined.

    Previously (Emacs 24.3 and earlier), while the byte-compiler would warn when you call a function as (no-such-function ...) and it is not defined or cleanly imported from another file, something like (mapcar #'no-such-function ...) would have produced only a runtime-error without a compile-time warning.

    The change means that both of these scenarios now produce compile-time warnings; however if you use (mapcar 'no-such-function ...) without using function-quoting then, once again, no compile-time warning can be generated. So the #' can help find errors (probably typos) early.

    Function-quoting also helps with a top-down programming style, since the byte-compiler will then list the functions you have not yet implemented (but will miss functions specified with a normal quote).

提交回复
热议问题