Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I sometimes hear the term "lexical variable?"

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?

like image 952
Tim Merrifield Avatar asked Dec 19 '08 17:12

Tim Merrifield


Video Answer


1 Answers

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.

like image 186
Doug Currie Avatar answered Nov 15 '22 08:11

Doug Currie