bbatsov / emacs-lisp-style-guide

A community-driven Emacs Lisp style guide
1.08k stars 53 forks source link

Proposal: prefer #' (sharp quote) when quoting function names #7

Closed skeeto closed 9 years ago

skeeto commented 10 years ago

When quoting symbols to access their function definitions, prefer the use of sharp quotes. This allows the bytecompiler to check if the function exists, issuing a warning just like it would when calling an undefined function.

This would change the anonymous function example.

(cl-remove-if-not #'evenp numbers)

Sharp quoting has become a lot more relevant with the introduction of lexical scoping.

(defun foo ()
  :global)

(cl-flet ((foo () :local))
  (list (funcall 'foo) (funcall #'foo)))
;; => (:global :local)
Malabarba commented 10 years ago

I was just about to recommend this as well. Emacs source does this AFAIK.

bbatsov commented 10 years ago

Agreed. Would you submit a PR Rregarding this?

On Thursday, September 4, 2014, Artur Malabarba notifications@github.com wrote:

I was just about to recommend this as well. Emacs source does this AFAIK.

— Reply to this email directly or view it on GitHub https://github.com/bbatsov/emacs-lisp-style-guide/issues/7#issuecomment-54543062 .

shosti commented 10 years ago

I'm not entirely sure about this--most examples from the manual don't sharp-quote (the Emacs source code seems pretty mixed). I think literally the only case where it makes a difference is cl-flet, and that has nothing to do with lexical scoping (it behaves the same with lexical scoping disabled). I guess there's no harm in it, but I'm just not sure it's important enough to be a hard recommendation.

skeeto commented 10 years ago

@bbatsov Sure thing!

@shosti The manual doesn't say sharp quoting is the same. It says it's syntax for function. It has a similar result but with subtle, important differences. When the manual is referring to global functions for key bindings (global-set-key, define-key) it uses a plain quote, which is reasonable. Otherwise the examples use sharp quote. Here's another example of where it's different than quoting. If this is the content of a file tmp.el:

(funcall #'foo)

When I byte compile it I get this:

tmp.el:3:1:Warning: the function `foo' is not known to be defined.

Change it to this and the warning goes away:

(defun foo ())
(funcall #'foo)

The sharp quote allowed the compiler to check my work for mistakes.

shosti commented 10 years ago

OK, that's convincing (I was mixing it up with the part in the manual about sharp-quoted lambda expressions, which are equivalent to non-quoted lambda expressions).