emacs-devel
[Top][All Lists]
Advanced

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

Re: Gnus using lexical-binding


From: Stefan Monnier
Subject: Re: Gnus using lexical-binding
Date: Sat, 30 Jan 2021 10:24:05 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

>> Btw, I think we could benefit from a detailed guide for how to convert
>> a package to lexical-binding, enumerating the steps to take, tools and
>> Emacs commands to use, possible problems and how to solve them, etc.
>> Somewhere in admin/notes/, perhaps?
>
> What about `(elisp) Converting to Lexical Binding'?
>
> Should we perhaps just expand that?  I'm sure third-party package
> developers could also benefit from any details we would like to add.

In terms of tools and commands, this section indeed describes what
I use.

I'm not sure how much further we want to go with that.
The main other tool I use is my intuition and various heuristics:

For the "unused var" warning, I can usually guess based on the name
whether it should have a `defvar` or not: variables whose name has
a "package prefix" should almost always have a `defvar` while those who
have a short name should almost always use lexical scoping.
Adding more `defvar`s than needed is largely harmless during conversion
(it's arguably harmful in the long run, but that can be addresses
later), so the main risk is for those variables with a non-prefixed name
where we normally presume they can use lexical scoping but which may
nevertheless require dynamic scoping.  Furthermore, some of those cases
can occur without triggering any warning from the byte-compiler because
the var is not locally unused.

This can happen typically with code like

    (let ((foo X)
          (bar Y))
      ...
      (eval config-var)
      ...)

where `config-var` is defined to hold an ELisp form which has access to
variables `foo` and `bar`.  If you're the package author or are a heavy
user of the package there's a chance you know about it and can take it into
account when performing the conversion, but otherwise it can be tricky
to figure it out.

The only way I know is to search for uses of `eval`, `symbol-value`,
`intern`, and `set` (this list is sadly not exhaustive, e.g. some uses
may occur via `run-hooks`) to try and find code that relies on access to
variables via dynamic scoping and then examine each one to try and see
which variables it may refer to, which is sometimes documented in
comments or docstrings.

This last part is tedious and rarely necessary, so I do it lazily,
i.e. when something indicates that it's probably necessary (e.g. because
of a regression found during testing, or because of some odd patterns in
the "unused variables" warnings, e.g. when the same set of unused
variable names appear a few times, tho sometimes these patterns are due
to copy&paste as well).


        Stefan




reply via email to

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