0

I've seen a few snippets of code where symbols were prepended by #' instead '. like the following snippet

(seq-count #'not (seq-mapn #'eq seq1 seq2))

What is the difference and why couldn't I just use ' instead of #'?

| improve this question | | | | |
  • 1
    Recommendation: Even though it often makes no difference (especially in the absence of lexical binding), just make a habit of function-quoting function symbols, always. At minimum it doesn't hurt and it's a nice visual hint that you're referring to a function. In some cases it's actually necessary. These days it's good style to use it always. – phils Nov 20 '19 at 21:02
0

Functionally, there is no difference in using #' instead of ' in elisp.

The main difference is that #' invokes the function function and ' calls quote, meaning that your lisp code can be written as:

(seq-count (function not) (seq-mapn (function eq) seq1 seq2))

There are many flavors of lisp beside emacs-lisp some of them have decided to differentiate between function symbols and variable symbols, placing them in different namespaces and thus allowing variables and functions to share names. In those cases, function is used to get the function symbol, whereas quote would give you the variable symbol.

This is not done in elisp however, which give you the same symbol in both cases. Thus, #' is primarily used to reinforce that the symbol refers to a function and not a variable, even if it's functionally the same.

I believe there are some cases where the optimizer can use the knowledge of #' to inline some functions, but I'm less certain on that topic.

| improve this answer | | | | |
  • 1
    The main functional difference between function and quote is inside certain cl-lib macros; see emacs.stackexchange.com/a/35991/15748. – Basil Nov 20 '19 at 10:36
  • So in the case of the cl library, elisp actually emulates this seperation between function and variable symbols? – GreenLlama Nov 20 '19 at 12:59
  • I don't think the optimizer distinguishes the two, no. There is a difference in terms of bytecompiler warnings since #' lets the compiler emit a warning if the function is not known to exist. – Stefan Nov 20 '19 at 14:12
0

In addition to what @Xalvew said, here's some information from elisp manual, detailing what differs between quote and function:

-- Special Form: function function-object

This special form returns FUNCTION-OBJECT without evaluating it. In this, it is similar to quote. 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.

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

The read syntax #' is a short-hand for using function.

| improve this answer | | | | |
  • So the difference is really just in optimization when writing pure elisp? – GreenLlama Nov 20 '19 at 12:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.