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 #'?
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 #'?
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.
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 unlikequote
, 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 usingfunction
.