help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Quoted function in `define-key'


From: Michael Heerdegen
Subject: Re: Quoted function in `define-key'
Date: Sun, 05 Feb 2017 14:50:36 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

Narendra Joshi <address@hidden> writes:

> So, the symbol `my-function' can have the function definition in its
> value slot?

There's a separate slot for function bindings.  You can refer to it
directly with passing the symbol to `symbol-function'.  So any symbol
can have two separate "bindings" - a binding as a variable, and one as a
function (unlike scheme, for example).  That's why Elisp is a Lisp-2.
Actually, there is one more cell for symbol property lists ("plists").
See

  (info "(elisp) Symbol Components")

for more details.


> What does the byte compiler do with this information? I am just
> curious about this.

When you sharp quote a lambda expression, the compiler knows that this
lambda list is supposed to be called as a function, and will byte
compile it.  The evaluator might turn it into a closure when lexical
binding is used.

In elisp, the `lambda' macro comes with implicit function quoting (see
its definition), so you can just omit it.

But OTOH, it's an error to `quote' lambdas like

  '(lambda () body...)

because it will prevent byte compilation or closure creation and make
Stefan angry.

Obviously for `function' quoted symbols there is not much to do, but the
byte compiler can determine whether that function is defined/ will be
defined at run time, so it can warn you about typos as an extra service.

For example, you get a compiler warning when compiling

  (define-key my-map [key] #'make-frame-cmomand)

but not for

  (define-key my-map [key] 'make-frame-cmomand)

> If this is recommended, I would also start quoting my functions as
> #'my-function.

Yes, it's a good idea.


Michael.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]