【问题标题】:Emacs: What does the "#" before function symbol imply? [duplicate]Emacs:函数符号前的“#”是什么意思? [复制]
【发布时间】:2017-04-11 05:19:53
【问题描述】:

例如:(add-hook 'after-init-hook #'global-flycheck-mode)

为什么# 需要添加到'global-flycheck-mode 前面?

【问题讨论】:

  • 闻起来像一个重复的问题,但我不知道如何在 Stack Exchange 中搜索 #'
  • 以及我标记的副本(并确保阅读 kdb 对此的回答),另请参阅“链接”标题下侧边栏中的许多其他问答(查看副本时) .但请记住,其他一些问答早于 #' 的一些用途!
  • @phils:你是怎么找到副本的?
  • 恐怕不是通过搜索#'。我们无法在有关编程的站点中搜索任意语法,这有点失败。 (在理论上 SymbolHound 可以做到这一点,但我刚才没有太多运气尝试。)我实际上搜索了“user:me function quoting”。知道我对此类问题至少有一个答案有助于缩小范围,但“[emacs] 函数引用”也会获得一些相关命中,您可以在浏览器的结果中搜索 #'。 (即使是C-s,如果您是Keysnail 用户:)

标签: emacs


【解决方案1】:

#' 只是使用function 的简写形式。来自 elisp 手册:

-- Special Form: function function-object
   This special form returns FUNCTION-OBJECT without evaluating it.
   In this, it is similar to ‘quote’ (see Quoting).  But unlike
   ‘quote’, it also serves as a note to the Emacs evaluator and
   byte-compiler that FUNCTION-OBJECT is intended to be used as a
   function.  Assuming FUNCTION-OBJECT is a valid lambda expression,
   this has two effects:

      • When the code is byte-compiled, FUNCTION-OBJECT is compiled
        into a byte-code function object (see Byte Compilation).

      • When lexical binding is enabled, FUNCTION-OBJECT is converted
        into a closure.  See Closures.

你可以在字节编译/加载这个时看到区别

 (setq f1 '(lambda (x) (* x x)))
 (setq f2 #'(lambda (x) (* x x)))

只有正确引用的形式是字节编译的:

(byte-code-function-p f1)
nil
(byte-code-function-p f2)
t

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 2013-01-26
    • 1970-01-01
    • 2017-02-08
    相关资源
    最近更新 更多