unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: Narendra Joshi <narendraj9@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Quoted function in `define-key'
Date: Sun, 05 Feb 2017 14:50:36 +0100	[thread overview]
Message-ID: <8760korbkj.fsf@drachen> (raw)
In-Reply-To: <87fujt7xsv.fsf@vicarie.i-did-not-set--mail-host-address--so-tickle-me> (Narendra Joshi's message of "Sun, 05 Feb 2017 15:40:40 +0530")

Narendra Joshi <narendraj9@gmail.com> 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	other threads:[~2017-02-05 13:50 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-04 10:20 Quoted function in `define-key' Narendra Joshi
2017-02-04 11:11 ` Narendra Joshi
2017-02-04 12:05 ` Michael Heerdegen
2017-02-04 15:03   ` Narendra Joshi
2017-02-04 17:25     ` Michael Heerdegen
2017-02-05 10:10       ` Narendra Joshi
2017-02-05 13:50         ` Michael Heerdegen [this message]
2017-02-05 19:09           ` Narendra Joshi
2017-02-05 19:45             ` Michael Heerdegen
2017-02-04 18:10 ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8760korbkj.fsf@drachen \
    --to=michael_heerdegen@web.de \
    --cc=help-gnu-emacs@gnu.org \
    --cc=narendraj9@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).