Why use #' before function arguments in emacs-lisp?

前端 未结 1 1423
轮回少年
轮回少年 2021-02-18 23:27

I\'m familiar with Emacs Lisp, but not Common (or any other) Lisp. Some Lisp programmers suggest (e.g. A basic function for emacs) that it\'s good to use #\' in fr

1条回答
  •  遇见更好的自我
    2021-02-19 00:04

    The quote character in #'foo has nothing to do with the one in 'foo.

    #'foo is replaced at read time by (function foo). When that is compiled and executed, it looks up the functional definition named by foo (by defun, flet, labels or similar).

    'foo is replaced at read time by (quote foo). When that is compiled and executed, it is simply replaced by the symbol foo.

    Funcall and apply (and thus generally higher order functions) take function designators as arguments. A function designator can be a function or a symbol naming a function, so both #'foo and 'foo are function designators.

    Therefore, the forms 'foo and #'foo seem interchangable at first glance. However, the lookup of the real function is done at different times—#'foo is looked up where it is invoked, while the function named by 'foo would only be looked up when it is finally applied.

    If you use a function designator many times over, it is much more efficient to do the lookup just once, or even just at compile time. That can be a really big timesaver, and translates to a smoother user experience in an editor.

    0 讨论(0)
提交回复
热议问题