0

For example: (add-hook 'after-init-hook #'global-flycheck-mode)

Why # needs to be prepended to 'global-flycheck-mode?

Jerry Zhang
  • 1,251
  • 2
  • 13
  • 15
  • Smells like a duplicate question, but I know no way to search for **`#'`** in Stack Exchange. – Drew Nov 27 '16 at 20:13
  • As well as the duplicate I've marked (and be sure to read kdb's answer to that), see also the numerous other Q&As in the sidebar (when viewing the duplicate) under the "Linked" heading. Bear in mind, though, that some of the other Q&As pre-date some of the uses of `#'` ! – phils Nov 27 '16 at 22:46
  • @phils: How did you find the duplicate? – Drew Nov 28 '16 at 03:07
  • Not by searching for `#'` I'm afraid. It's a bit of a fail that we can't search for arbitrary syntax in a site about programming. (In *theory* [SymbolHound](http://symbolhound.com/) can do this, but I didn't have much luck trying that just now.) I actually searched for "user:me function quoting". Knowing that I had at least one answer to such a question helped for narrowing that down, but "[emacs] function quoting" gets some relevant hits as well, and you can search for `#'` in the results in your browser. (With `C-s` even, if you're a [Keysnail](https://github.com/mooz/keysnail/wiki) user :) – phils Nov 28 '16 at 03:40

1 Answers1

1

#' is just a short-hand for using function. From the elisp manual:

-- 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.

You can see the difference when byte-compiling/loading this

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

Only the the correctly quoted form is byte-compiled:

(byte-code-function-p f1)
nil
(byte-code-function-p f2)
t
Jürgen Hötzel
  • 18,997
  • 3
  • 42
  • 58