unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Canonical add-hook idiom
@ 2003-11-15  5:35 Bob Nelson
  2003-11-15  8:25 ` Oliver Scholz
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Bob Nelson @ 2003-11-15  5:35 UTC (permalink / raw)


What is the preferred approach given these examples:

    1). (add-hook 'some-mode-hook (lambda () [...]
    2). (add-hook 'some-mode-hook '(lambda () [...]
    3). (add-hook 'some-mode-hook (function (lambda () [...]
    4). None of the above -- please explain.

-- 
=============================================================================
           Bob Nelson -- Dallas, Texas, USA (nelsonbe@earthlink.net)
          http://www.oldradio.com/archives/nelson/open-computing.html
Good engineering is finding the right wrench to pound in the correct screw

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Canonical add-hook idiom
  2003-11-15  5:35 Canonical add-hook idiom Bob Nelson
@ 2003-11-15  8:25 ` Oliver Scholz
  2003-11-15 18:27   ` Henrik Enberg
  2003-11-15 16:55 ` Jesper Harder
  2003-11-16 23:26 ` Stefan Monnier
  2 siblings, 1 reply; 5+ messages in thread
From: Oliver Scholz @ 2003-11-15  8:25 UTC (permalink / raw)


Bob Nelson <bnelson@nelsonbe.com> writes:

> What is the preferred approach given these examples:
>
>     1). (add-hook 'some-mode-hook (lambda () [...]
>     2). (add-hook 'some-mode-hook '(lambda () [...]
>     3). (add-hook 'some-mode-hook (function (lambda () [...]
>     4). None of the above -- please explain.
[...]

I definitely prefer (1).

Some would prefer #'(lambda () ...), which for the Lisp interpreter
is the same as (3).  But AFAIK this is just a Common Lisp idiom and
it makes no difference in Emacs Lisp.

    Oliver
-- 
25 Brumaire an 212 de la Révolution
Liberté, Egalité, Fraternité!

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Canonical add-hook idiom
  2003-11-15  5:35 Canonical add-hook idiom Bob Nelson
  2003-11-15  8:25 ` Oliver Scholz
@ 2003-11-15 16:55 ` Jesper Harder
  2003-11-16 23:26 ` Stefan Monnier
  2 siblings, 0 replies; 5+ messages in thread
From: Jesper Harder @ 2003-11-15 16:55 UTC (permalink / raw)


Bob Nelson <bnelson@nelsonbe.com> writes:

> What is the preferred approach given these examples:
>
>     1). (add-hook 'some-mode-hook (lambda () [...]
>     2). (add-hook 'some-mode-hook '(lambda () [...]
>     3). (add-hook 'some-mode-hook (function (lambda () [...]
>     4). None of the above -- please explain.

1) or 4).

It's sometimes better to avoid lambda and add a function instead.
This makes it easier to remove it from the hook again:

  (add-hook 'foo-hook 'bar)
  ;; Oops, bar didn't work as expected:
  (remove-hook 'foo-hook 'bar)

Removing isn't quite as easy for lambda's.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Canonical add-hook idiom
  2003-11-15  8:25 ` Oliver Scholz
@ 2003-11-15 18:27   ` Henrik Enberg
  0 siblings, 0 replies; 5+ messages in thread
From: Henrik Enberg @ 2003-11-15 18:27 UTC (permalink / raw)


Oliver Scholz <alkibiades@gmx.de> writes:

> Bob Nelson <bnelson@nelsonbe.com> writes:
>
>> What is the preferred approach given these examples:
>>
>>     1). (add-hook 'some-mode-hook (lambda () [...]
>
> I definitely prefer (1).
>
> Some would prefer #'(lambda () ...), which for the Lisp interpreter
> is the same as (3).  But AFAIK this is just a Common Lisp idiom and
> it makes no difference in Emacs Lisp.

In addition using #'(lambda () ...) or '(lambda () ...) means the lambda
form won't be compiled if the file is.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Canonical add-hook idiom
  2003-11-15  5:35 Canonical add-hook idiom Bob Nelson
  2003-11-15  8:25 ` Oliver Scholz
  2003-11-15 16:55 ` Jesper Harder
@ 2003-11-16 23:26 ` Stefan Monnier
  2 siblings, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2003-11-16 23:26 UTC (permalink / raw)


> What is the preferred approach given these examples:
>     1). (add-hook 'some-mode-hook (lambda () [...]
>     2). (add-hook 'some-mode-hook '(lambda () [...]
>     3). (add-hook 'some-mode-hook (function (lambda () [...]
>     4). None of the above -- please explain.

The '(lambda form is to be avoided: the ' says "what follows
is pure data" whereas what follows is actually code, so it's
confusing (I expect humans won't get confused, but byte-compilers
do get confused).

`lambda' is a macro that expands to (function (lambda so the two are
pretty much interchangeable.  As for #'(lambda, it is a shorthand
for (function (lambda.

I thus recommend (lambda since it's the shortest and ask people to stay away
from '(lambda since it poses problems to the byte-compiler (as well as to
other code-walkers such as some fancy CL macros (typically
`lexical-let')).  But if you prefer #(lambda or (function (lambda,
that's OK: you won't burn in hell for it.


        Stefan

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-11-16 23:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-15  5:35 Canonical add-hook idiom Bob Nelson
2003-11-15  8:25 ` Oliver Scholz
2003-11-15 18:27   ` Henrik Enberg
2003-11-15 16:55 ` Jesper Harder
2003-11-16 23:26 ` Stefan Monnier

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).