bug#81564: Possible bug when binding dynamically with an existing lexical binding

Thuna <[email protected]>
Newsgroups gmane.emacs.bugs
Message-ID <[email protected]>
>   > >   (let ((x 2)) (defvar x) (let ((x 10)) x))
>
> Is it ever reasonable to use defvar inside of let?
> It sure seems weird.

I'm unsure what part specifically you are asking about, but using the
unary `defvar' as a non-toplevel form to declare the symbol as dynamic
locally is definitely legal:

> If INITVALUE is missing, the form marks the variable "special" locally
> (i.e., within the current lexical scope, or the current file, if the
> form is at top-level) [...]

as well as the existence of `dlet'.

It's unclear if the unary `defvar' is allowed when the symbol is
let-bound, but it is explicitly allowed (or accounted for) in the
non-unary case:

> If SYMBOL is let-bound, then this form does not affect the local let
> binding but the toplevel default binding instead, like
> ‘set-toplevel-default-binding‘.

Ideally I would want the behavior of locally special variables to work
as in Common Lisp[1] or at least be capable of doing so.  I don't think
it's likely, since introducing a proper `declare' is too much a hurdle
for just this issue, and the current behavior of `defvar' can't be changed in
a backward-compatible way while supporting such semantics, but I just
want to throw it out there in case there _is_ some way that I am not
thinking of.

[1] https://www.lispworks.com/documentation/HyperSpec/Body/d_specia.htm
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.