unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Artur Malabarba <bruce.connor.am@gmail.com>
To: Robin Templeton <robin@terpri.org>
Cc: emacs-devel <emacs-devel@gnu.org>
Subject: Re: add-hook and defvar
Date: Mon, 23 Feb 2015 05:00:56 +0000	[thread overview]
Message-ID: <CAAdUY-+zGXMOmKMh2+kUEmiRYzWaGzh2Jwrt2oBELHrAz5UExA@mail.gmail.com> (raw)
In-Reply-To: <87y4nqopup.fsf@panthera.terpri.org>

[-- Attachment #1: Type: text/plain, Size: 2745 bytes --]

On Feb 21, 2015 8:35 PM, "Robin Templeton" <robin@terpri.org> wrote:
>
> Artur Malabarba <bruce.connor.am@gmail.com> writes:
>
> > While it's nice that `add-hook' tries to do the right thing even when
> > the variable isn't defined yet, that's a bit of a false assurance.
> > Although it's documented in the docstring, it seems undesirable that
> > it sets the variable. This means that if the hook's defvar were to
> > specify an initial value, this value won't take effect because of that
> > preceding `add-hook' invocation.
>
> This seems like a bug in the program defining the hook variable, not a
> problem with `add-hook' per se; programs should always allow for the
> possibility that the user has set their own value for a variable and
> that the default value in the defvar form is ignored.

The problem is not that the hook will stop working if it gets overriden,
it's more about the fact that the the user probably doesn't know that
`add-hook' is actually behaving like `override-hook'. I've been a
user/developer for years and I only just found out this year.

The fact that add-hook works even before the variable is defined gives the
impression that it's always safe to use. But it's actually quite the
opposite. It would have just been better if it behaved like all other
functions of this type, throwing an error if the variable isn't defined.

>I would expect the
> program to call `add-hook' itself if the default hook functions were
> important.

I'm not too worried about hook-functions that are vital, as the developer
can always just move these out of the hooks. But I am worried that the
developer trying to provide default behaviour for hooks has no sage way of
doing that.

If the program calls add-hook as you suggest, then the user who actually
*wants* to completely override the hook (by using `setq') won't be able to,
because you're manually adding these other values to it.

Defining a variable with an initial value is the standard way of offering a
default behaviour. And it's a problem that such a widely used function
doesn't play well with that.

> Another potential problem is that `add-hook' followed by `run-hooks'
> will do nothing if the hook variable has not been defined.

Yes, but isn't it a bug to run-hooks a variable that wasn't explicitly
defined?

> If the
> behavior of `add-hook' were changed, I would prefer it if it simply
> required the variable to be defined; that would avoid the above
> disadvantages, would make it consistent with functions like
> `add-to-list', and would normally ensure that the hook is initialized as
> expected.

I'm fine for that too. We could also provide a new function like
add-to-hook in order to provide backwards compatibility, and slowly
obsolete add-hook.

[-- Attachment #2: Type: text/html, Size: 3387 bytes --]

  reply	other threads:[~2015-02-23  5:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-21 19:55 add-hook and defvar Artur Malabarba
2015-02-21 21:05 ` Drew Adams
2015-02-21 22:35 ` Robin Templeton
2015-02-23  5:00   ` Artur Malabarba [this message]
2015-02-22 19:18 ` Stefan Monnier
2015-02-23  5:10   ` Artur Malabarba
2015-02-23 22:31     ` Stefan Monnier
2015-02-24  6:29       ` Philipp Stephani
2015-02-25  2:56         ` Stefan Monnier
2015-03-05 20:28           ` Philipp Stephani
2015-03-05 21:45             ` Artur Malabarba
2015-03-06 19:05               ` Philipp Stephani
2015-03-06 20:27                 ` Artur Malabarba
2015-03-10  2:14             ` 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=CAAdUY-+zGXMOmKMh2+kUEmiRYzWaGzh2Jwrt2oBELHrAz5UExA@mail.gmail.com \
    --to=bruce.connor.am@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=robin@terpri.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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