算的,但是这个问题的情况似乎比较特殊。
考虑这个简单的例子:(lexical-binding默认开启)
(let ((x 1))
(lambda () x))
求值得到#[nil (x) ((x . 1))],let的词法绑定生效。
但是如果前面加上
(defvar x 0)
(let ((x 1))
(lambda () x))
得到是#[nil (x) (t)]。x变成了动态绑定的,let的词法绑定不生效。
我想这是defvar的问题。defvar的文档里说:
If INITVALUE is provided, the ‘defvar’ form also declares the variable as “special”, so that it is always dynamically bound even if ‘lexical-binding’ is t. 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), and does nothing if ‘lexical-binding’ is nil.
那么defcustom是不是有这个问题呢?它的文档里说:
This macro uses
defvar' as a subroutine, which also marks the variable as \"special\", so that **it is always dynamically bound** **even whenlexical-binding’ is t**.
相关问题我看到有一个相关的帖子之前讨论过: