为什么我有时会听到“词汇变量”这个术语?
我见过几次“词法变量”这个术语,主要是在闭包的上下文中。 Paul Graham 在他的 Lisp 书籍中使用这个术语来指代使用 let 表达式定义的变量。
据我所知,词法作用域是静态作用域的另一个名称。 词法变量只是程序单元引用环境中可见的变量吗?
我希望在这个假期用这个词给我的朋友和家人留下深刻的印象,有人可以帮助我吗?
I've seen the term "lexical variable" a few times, mostly in the context of closures. Paul Graham uses the term in his books on Lisp referring to variables defined using the let expression.
I understand that lexical scoping is another name for static scoping. Is lexical variable just a variable that is visible in a program unit's referencing environment?
I hope to use this term to impress my friends and family this holiday season, can someone help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
词法变量是只能在其词法范围内(通过名称)引用的变量。 换句话说,变量的范围是由程序文本定义的,而不是由程序执行的动态定义的。 变量和绑定到它的值可能具有超出词法范围的范围(寿命),例如,如果它是在闭包中捕获的。
请参阅范围和程度的描述。
A lexical variable is a variable that can only be referenced (by name) within its lexical scope. In other words, the scope of the variable is defined by the text of the program, not the dynamics of the program's execution. The variable and the value bound to it may have extent (life) beyond the lexical scope, e.g., if it is captured in a closure.
See this description of scope and extent.
词法变量在程序单元的引用环境中可见,但并非所有在程序单元的引用环境中可见的变量都一定是词法变量。
基本上,词法变量是特定于其定义范围的变量。 在像 Perl 这样的语言中,本质上有两个相互叠加的命名空间:底层动态包命名空间和覆盖的词法命名空间。 因此 $foo 可以引用其中任何一个,这仅取决于它是否已按词法声明。
A lexical variable is visible in a program unit's referencing environment, but not all variables that are visible in a program unit's referencing environment are necessarily lexical variables.
Basically, a lexical variable is one that is specific to the scope where it is defined. In a language like Perl, you essentially have two namespaces masked on top of each other: the underlying dynamic package namespace, and the overlaid lexical namespace. So $foo could refer to either, depending solely on whether it's been declared lexically.