emacs-devel
[Top][All Lists]
Advanced

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

Re: cl-eval-when -- A workaround for recursive require?


From: Stefan Monnier
Subject: Re: cl-eval-when -- A workaround for recursive require?
Date: Fri, 29 Apr 2022 13:36:41 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Zhu Zihao [2022-04-27 18:52:07] wrote:
> I investigated into the `cl-eval-when` a time long ago about this
> `cl-eval-when` hack in the source of magit here.
> https://github.com/magit/magit/blob/master/lisp/magit.el#L641
>
> A breif summary: magit.el use a `cl-eval-when` block with load time and
> eval time only evaluation to require its sub-components, while each
> sub-component use `(require 'magit)` to use procedure in different
> sub-components. This hack seems to be a hack to avoid recursive require.

This kind of setup is quite common, but the resulting cyclic
dependencies tend to be a nightmare.

> The result of my investigation I remember is: "Don't use cl-eval-when,
> it's not robust for many eval strategy combination. eval-when-compile or
> eval-and-compile are more reasonable alternatives."

I tend to agree.  But when it comes to circular dependencies like that
of `magit.el` the use of `cl-when-eval` is a minor detail.

> The bytecomp.el file is a monolith and I don't know how to read and
> understand it. Curiosity drives me to ask a question here: How does
> `cl-eval-when` hack works for recurisve require? Is it a ugly hack?

There's nothing special about `cl-eval-when` for cyclic dependencies.
Some parts of `cl-eval-when` rely on ugly hacks, but I don't think it
makes much difference here.

The only sane way to solve these problems is to fix the circular
dependencies, really.

Often one of the best tools for that is to rely on autoloads instead of
`require` for some functions, and the better way to do that is to use an
"internal" autoloads file (i.e. one that's separate from the
`<pkg>-autoloads.el` loaded at startup and that's only loaded when the
package is actually used).

See lisp/**/*-loaddefs.el for examples of such "internal"
autoloads files.

We don't have great support for auto-generation of such files, tho, so
sometimes it requires manual work to set it up (if you search for
`;;;###` in `lisp/Makefile.in` you'll see examples of what I'm
referring to).


        Stefan




reply via email to

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