1

Are 'function-name and (function function-name) completely equivalent, or are there conditions under which I must use the more verbose (function function-name)?

(apply 'message ( list "call func via quote"))
(apply (function message) (list "call func via function keyword"))

Both of them yield the same effect. But in many elisp libraries, i am seeing people using (function func) to pass function symbols like in this example.

(defun dired-add-file (filename &optional marker-char)
  (dired-fun-in-all-buffers
   (file-name-directory filename) (file-name-nondirectory filename)
   (function dired-add-entry) filename marker-char))

Why do people use more verbose way, when they can use just a quote?

2
  • I rephrased the question somewhat so that it would not get dinged for subjectivity. If I misunderstood your intent, we can roll back the edit.
    – Dan
    Sep 22, 2016 at 0:41
  • This is a nice explanation on the difference between #' (aka function) and ': endlessparentheses.com/…. Basically, as C-h f function says: "In byte compilation, function causes its argument to be compiled. quote can not do that. IMHO, just use #' rather than '.
    – omajid
    Sep 22, 2016 at 1:08

1 Answer 1

3

'foo and (quote foo) are identical: they prevent evaluation of foo.

#'foo and (function foo) are also identical: they extract the function binding of foo, including at compile time, which means that the function gets compiled.

So, if you want a symbol, use 'foo:

(make-variable-buffer-local 'my-local-var)

if you want a function, use #'foo:

(setq my-after-function #'my-function)

or

(apply #'my-function arg1 arg2 others)

See also

  1. When to use 'quote in Lisp
  2. Why sharp quote lambda expressions?
3
  • #'foo and (function foo) don't extract the function binding, they just return a symbol. Just try this to get convinced: (type-of (function type-of)) (type-of (symbol-function (function type-of)))
    – memeplex
    Nov 29, 2018 at 2:55
  • @memeplex: only in Emacs Lisp and only at this time. Things may change. ;-)
    – sds
    Nov 29, 2018 at 3:01
  • @memeplex: even simpler: (eq 'type-of (function type-of)) => t ;)
    – duthen
    Sep 19, 2022 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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