The sharp quote (or function quote, or simply #') is an abbreviation
for the function form. It is essentially a version of quote (or ')
which enables byte-compilation, but its actual usefulness has changed
throughout the years.
A little over two decades ago, it was used to quote lambda forms.
You see, '(lambda (x) (* x x)) was just a raw list to the
byte-compiler, but #'(lambda (x) (* x x)) was an actual function
that could be compiled. Now-a-days—or rather, now-a-decades—the
lambda form sharp-quotes itself, meaning a plain (lambda (x) (* x
x))is identical to the #' version. In fact, you should never
quote your lambdas with either quotes.
On the other hand, just as you'd expect the sharp quote to become
redundant for the elisp programmer, a new use arises for it. The
compiler throws a warning whenever it notices you've used an undefined
function, say (not-defined "oops"), but it can't do the same for
something like (mapcar 'not-defined some-list) because it doesn't
know that symbol is the name of a function. The sharp quote is a way
of conveying that information to the compiler, so if it runs into
(mapcar #'not-defined some-list), it can throw a warning
accordingly.
So it is always good practice to sharp quote every symbol that is the
name of a function, whether it's going into a mapcar, an apply, a
funcall, or anything else. Adhering to this actually unearthed a
small bug in one of my packages.
And of course, we can make things more convenient.
(defunendless/sharp()"Insert #' unless in a string or comment."(interactive)(call-interactively#'self-insert-command)(let((ppss(syntax-ppss)))(unless(or(eltppss3)(eltppss4)(eq(char-after)?'))(insert"'"))))(define-keyemacs-lisp-mode-map"#"#'endless/sharp)
Get in the habit of using sharp quote
10 Nov 2014, by Artur Malabarba.The sharp quote (or function quote, or simply
#'
) is an abbreviation for thefunction
form. It is essentially a version ofquote
(or'
) which enables byte-compilation, but its actual usefulness has changed throughout the years.A little over two decades ago, it was used to quote
lambda
forms. You see,'(lambda (x) (* x x))
was just a raw list to the byte-compiler, but#'(lambda (x) (* x x))
was an actual function that could be compiled. Now-a-days—or rather, now-a-decades—the lambda form sharp-quotes itself, meaning a plain(lambda (x) (* x x))
is identical to the#'
version. In fact, you should never quote your lambdas with either quotes.On the other hand, just as you'd expect the sharp quote to become redundant for the elisp programmer, a new use arises for it. The compiler throws a warning whenever it notices you've used an undefined function, say
(not-defined "oops")
, but it can't do the same for something like(mapcar 'not-defined some-list)
because it doesn't know that symbol is the name of a function. The sharp quote is a way of conveying that information to the compiler, so if it runs into(mapcar #'not-defined some-list)
, it can throw a warning accordingly.So it is always good practice to sharp quote every symbol that is the name of a function, whether it's going into a
mapcar
, anapply
, afuncall
, or anything else. Adhering to this actually unearthed a small bug in one of my packages.And of course, we can make things more convenient.
Tags: style, keybind, lisp, init.el, emacs,
New in Emacs 25.1: Query-replace history is enhanced. »
« Super Smart Capitalization
Related Posts
A few paredit keys that take over the world in keybind
Fill and unfill paragraphs with a single key in keybind
Disable Mouse only inside Emacs in keybind
Content © 2019, All rights reserved. Icons under CC3.0.