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

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

Re: the hand that feeds you


From: Kevin Rodgers
Subject: Re: the hand that feeds you
Date: Thu, 27 Jul 2006 17:43:27 -0600
User-agent: Thunderbird 1.5.0.4 (Windows/20060516)

Harald Hanche-Olsen wrote:
+ Andreas Seik <andreas_neu@gmxpro.de>:

| Hello Newsgroup,
|
| i need code in a hook that is executed only once,
| so i tested the following:
|
|
| (defvar my-hook '())
| (defun kill-myself ()
|  (insert "only once")(remove-hook 'my-hook 'kill-myself))
| (add-hook 'my-hook 'kill-myself)
| (run-hooks 'my-hook);; i think it might crash here, but it does not
| (run-hooks 'my-hook)
|
| it, seems to work, but i do not trust it.
| is it not "biting the hand that feeds you"

It works, even in the presence of more hooks on the hook variable.
But I agree it looks scary, and it seems to rely on the implementation
of run-hooks and remove-hook:  In all likelihood, run-hooks will just
cdr down the list, running the car of the rest each time.  And
remove-hook will not modify the cons cell containing kill-myself as
its car and the remaining list as its cdr, so all is well.

But it's bad coding style, because you must reason out all that to see
why it works.  And it is conceivable, if unlikely, that the
implementation one of these functions will change so that it no longer
works.  Better then, to let kill-myself really to commit suicide,
rather than just to remove itself from a hook:

(defun kill-myself ()
  (insert "only once")
  (defun kill-myself ()))

That is just as dependent on the implementation of defun as the original
is on the implementation of run-hooks and remove-hooks, isn't it?
Although I guess you could argue that it's only fragile with respect to
1 special form instead of 2 functions.

BTW, this is truly suicidal:

(defun kill-myself ()
  (insert "only once")
  (unintern 'kill-myself))

But maybe (fmakunbound 'kill-myself) or
(fset 'kill-myself (symbol-function 'ignore)) is good enough.  :-)

All I wanted was a Pepsi,
--
Kevin





reply via email to

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