emacs-devel
[Top][All Lists]
Advanced

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

Re: add-hook and defvar


From: Artur Malabarba
Subject: Re: add-hook and defvar
Date: Mon, 23 Feb 2015 05:00:56 +0000


On Feb 21, 2015 8:35 PM, "Robin Templeton" <address@hidden> wrote:
>
> Artur Malabarba <address@hidden> 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.


reply via email to

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