[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: add-to-list with lexical variables
From: |
Stefan Monnier |
Subject: |
Re: add-to-list with lexical variables |
Date: |
Sat, 08 Jun 2013 16:34:49 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) |
>>> 1. when I set `lexical-binding' to t and byte-compile the file, it
>>> would report this error:
>>>
>>> add-to-list cannot use lexical var `env-list'
add-to-list is a function, and functions have access to the
dynamically-bound vars of its caller, but not to the lexically-bound
vars of its caller.
The warning was added because add-to-list is a frequent use-case where
Elisp code presumes dynamic binding.
>>> 2. And when I using `lexical-let*' instead, there would be an warning:
>>>
>>> Warning: assignment to free variable `env-list'
lexical-let is an old way to get lexical binding. It was added long
before add-to-list was invented, and it was only used at those few
places where people really wanted lexical binding, and those people
generally knew that add-to-list couldn't possibly work there, so the use
of add-to-list on a var bound with lexical-let was so rare that it
didn't occur to anybody to try and provide an ad-hoc check and warning.
instead it just notices that the call to `add-to-list' uses a (dynamic)
variable `env-list' which is not declared (i.e. is "free").
> However I still don't know the difference `lexical-binding' and
> `lexical-let' brings. Are there some authoritative introductions/tutorials?
lexical-let is an old hack made obsolete by lexical-binding.
Stefan