[Top][All Lists]

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

Re: Real constants

From: Stefan Monnier
Subject: Re: Real constants
Date: Wed, 13 Jul 2005 22:25:40 -0400
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

> Yeah, I meant "trivial now", of course. But that's been in Emacs for
> what? Five years?

There's no released Emacs with defvaralias, so it's still recent.

>> Among the local patches I use, I've expanded this `constant' field to
>> 2 bits, so it can be either hard-read-only or soft-read-only (or writable,
>> of course).  The soft-read-only state is used by defconst so that variables
>> defined by defconst cannot be setq but can be changed via defconst
>> (important when you reload a new version of a package where a defconst was
>> changed).

> Do you allow un-const'ing things?

No.  But I could make `defvar' un-const a defconst if that's necessary.

> If so, special care should be taken with `enable-multibyte-characters',
> nil, t and perhaps a few others...

These are hard-read-only so they can't be redefined with defconst
or un-const'd.

>> Making defconst variables into constants introduces a few occasional
>> problems with some packages, of course, but nothing too hard to fix.

> And in fact I'd bet most of these problems are, if not bugs, at least
> bad style... :)

Yes, most.  But for some the fix is not too elegant either.
Typical problems:

- The const-value can't be conveniently constructed at top-level:

   (defvar foo nil)
   (defvar bar nil)
   (let ((blabla))
     (setq foo (toto blabla))
     (setq bar (toto blabla)))

  Removing the defvars and replacing the setq with defconst is correct, but
  the byte-compiler is a bit dumb and don't realize that foo and bar are
  both unconditionally defined, so it may warn of unknown variables.

- name clashes.  E.g.

    (defconst e 2.718281828459045)
    (let ((b (match-beginning 0))
          (e (match-end 0)))

- there are also other cases where a const-var is not quite constant: some
  part of the code (or some advice, or hook) wants to temporarily let-bind
  it to some other value.

> Could you please post your code here? I think it would be a great
> feature (after 22.1).

I doubt it'll ever make its way into Emacs: it introduces compatibility
problems without bringing any clear benefit.


reply via email to

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