什么时候应该使用 Emacs #'function 语法? [英] When should Emacs #'function syntax be used?

查看:15
本文介绍了什么时候应该使用 Emacs #'function 语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我什么时候应该使用 Emacs Lisp 的 function 过程?如果您将函数作为参数 'like-this#'like-this 传递,我还没有发现任何示例中的行为会有所不同.事实上,如果我评估 (eq 'goto-char #'goto-char) 它返回 t.

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 arguments 'like-this or #'like-this. In fact, if I evaluate (eq 'goto-char #'goto-char) it returns t.

我遇到的Emacs Lisp 代码很少使用function/#';作者只是引用/'一切.
示例:(add-hook 'emacs-lisp-hook 'turn-on-eldoc-mode)

The Emacs Lisp code that I've come across rarely uses function/#'; the authors just quote/' everything.
Example: (add-hook 'emacs-lisp-hook 'turn-on-eldoc-mode)

但是,我可以找到一些反例.这是来自 Emacs 24.3 的 electric.el 的源代码之一:

However, I can find a few counterexamples. Here's one from the source code of Emacs 24.3's electric.el:

(add-hook 'post-self-insert-hook
          #'electric-indent-post-self-insert-function
          'append)

猜测和进一步的问题:

  • 这只是 Lisp-2 的风格约定吗?
  • 是否与字节编译有关?
  • 它只对图书馆作家重要吗?比如,如果您打算让您的代码在大量环境和 Emacs 版本下运行?(推论是,如果您只是修补点文件",那么您无需担心所有这些.)
  • 什么时候应该引用 lambda 表达式?我什么时候可以不引用它们?
    例如,(do-something '(lambda ...
    versus (do-something (lambda ...
  • 早期版本的 Emacs 中是否存在某些限制导致了 elisp 的这些方面?就像,只要我使用的是比 X 更新的 Emacs 版本,我是否可以忽略 '#' 之间的区别?
  • Guesses and further questions:

    • Is it just a Lisp-2 stylistic convention?
    • Does it have to do with byte-compilation?
    • Does it only matter for library writers? Like, if you intend for your code to be run under a huge number of environments and Emacs versions? (The corollary would be if you're just "dotfile-tinkering" then you don't need to worry about all this.)
    • When should I quote lambda-expressions? When can I leave them unquoted?
      E.g., (do-something '(lambda …
      versus (do-something (lambda …
    • Was there some limitation in an earlier version of Emacs that gave rise to these facets of elisp? Like, can I mostly ignore the difference between ' and #' as long as I'm using a version of Emacs more recent than X?
    • 推荐答案

      function (aka #') 用于引用函数,而 quote(又名 ')用于引用数据.现在,在 Emacs-Lisp 中,一个函数单元是函数的符号本身就是一个函数,所以 #'symbol 在实践中与 'symbol 是一样的(虽然意图是不同的,第一个明确指出,人们不只是在谈论符号symbol",而是在谈论名为symbol"的函数).

      function (aka #') is used to quote functions, whereas quote (aka ') is used to quote data. Now, in Emacs-Lisp a symbol whose function cell is a function is itself a function, so #'symbol is just the same as 'symbol in practice (tho the intention is different, the first making it clear that one is not just talking about the symbol "symbol" but about the function named "symbol").

      区别不仅仅在于文体的地方在于引用 lambdas 时:'(lambda ...) 是一个表达式,其计算结果为 list,其第一个元素是符号 lambda.你可以对它应用诸如 carcdr 之类的东西,但你不应该把它当作一个函数来调用(尽管在实践中它往往工作得很好).相反 #'(lambda ...)(可以只写成 (lambda ...))是一个计算结果为 function.这意味着您不能将 car 应用于它,但是字节编译器可以查看 #'(lambda ...) 内部,在其中执行宏扩展,警告如果它发现的东西看起来不像犹太洁食等等......;对于词法绑定,它甚至必须查看内部以找到该函数所引用的自由变量.

      The place where the difference is not just stylistic is when quoting lambdas: '(lambda ...) is an expression which evaluates to a list whose first element is the symbol lambda. You're allowed to apply things like car and cdr to it, but you should not call it as if it were a function (although in practice it tends to work just fine). On the contrary #'(lambda ...) (which can be written just (lambda ...)) is an expression which evaluates to a function. That means you can't apply car to it, but the byte-compiler can look inside #'(lambda ...), perform macro-expansion in it, warn you if what it finds doesn't look kosher, etc...; For lexical-binding it even has to look inside in order to find the free variables to which that function refers.

      这篇关于什么时候应该使用 Emacs #'function 语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆