Lexical scoping (sometimes known as static scoping ) is a convention used with many programming languages that sets the scope (range of functionality) of a variable so that it may only be called (referenced) from within the block of code in which it is defined. The scope is determined when the code is compiled. A variable declared in this fashion is sometimes called a private variable.
Questions tagged [lexical-scope]
271 questions
6
votes
2 answers
How does JavaScript's lexical environment maintain variables declarations within nested block scopes?
I've read a couple of more comprehensive articles on the execution context and now I am sort of confused and messed up in the head.
To keep the question as brief as possible avoiding long citations I better try to illustrate my mental model through…

fekaloid
- 195
- 1
- 2
- 9
6
votes
4 answers
Defining "let expressions" in Prolog
In many functional programming languages, it is possible to "redefine" local variables using a let expression:
let example =
let a = 1 in
let a = a+1 in
a + 1
I couldn't find a built-in Prolog predicate for this purpose, so…

Anderson Green
- 30,230
- 67
- 195
- 328
6
votes
2 answers
In LISP how to inspect free variables in a closure?
In lisp I can bind free variables bound in a closure like this...
(let ((x 1) (y 2) (z 3))
(defun free-variables () (+ x y z)))
(free-variables)
results in ...
6
What I want to know is if it is possible to inspect bound closure variables…

Reuben Peter-Paul
- 1,550
- 3
- 15
- 25
6
votes
2 answers
Is it possible to create new lexical symbols in other contexts at runtime?
I'm writing a module for creating enums with custom behaviour. What I do at the moment is add the enum to the GLOBAL package, but that doesn't install any lexical symbols unless you create the enum in one module and import it from another. Using…

Kaiepi
- 3,230
- 7
- 27
6
votes
2 answers
Recursive closure in JavaScript
function buildList( list ) {
var i = 0;
var first = function () {
console.log( "in" )
console.log( i );
}
var Second = function () {
console.log( "out" )
first();
}
return Second;
}
var a = buildList( [1, 2, 3]…

Parshuram Kalvikatte
- 1,616
- 4
- 20
- 40
6
votes
2 answers
Why variable object was changed to lexical environment in ES5?
ES5 changed variable object(VO) to lexical environment. What's the motivation of such change since VO is already very obvious as perception?

Thomson
- 20,586
- 28
- 90
- 134
6
votes
2 answers
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…

Tim Merrifield
- 6,088
- 5
- 31
- 33
6
votes
1 answer
Does my $_; do anything if $_ is implied
I think the answer is yes but I just want to make sure. so if I have
sub something {
my $_;
my @array = ...;
while ( @array ) {
say;
}
}
is the my $_; actually effective at lexicalizing the parameter passed to the say?
In…

xenoterracide
- 16,274
- 24
- 118
- 243
6
votes
4 answers
Lexically importing useful functions in a big script
Sometimes I need a useful utility function, like List::Util::max in the middle of a large program that does lots of stuff. So if I do
use List::Util 'max';
At the top of my program, I'm stuck with that symbol, polluting my whole namespace, even…

friedo
- 65,762
- 16
- 114
- 184
6
votes
3 answers
Penetrating the `set-process-sentinel` hierarchy with let-bound variables
I have never been able to come up with a method to penetrate the set-process-sentinel hierarchy with let-bound variables defined at the outset of the function -- only buffer-local or global variables can penetrate it. Let-bound variables can reach…

lawlist
- 13,099
- 3
- 49
- 158
6
votes
2 answers
Lexical scoping vs dynamic scoping
So I have this problem where I have to figure out the output using two different scoping rules. I know the output using lexical scoping is a=3 and b=1, but I am having hard time figure out the output using dynamic scoping.
Note:the code example…

Samson
- 133
- 2
- 7
6
votes
1 answer
blocks don't see methods (chef resources)
Let's say we have two resources:
template 'template1' do
owner 'root'
group 'root'
end
template 'template2' do
owner 'root'
group 'root'
end
I'd like to reuse code inside resources. However, if I define a proc in the recipe, you get a…

m33lky
- 7,055
- 9
- 41
- 48
5
votes
4 answers
Trying to localize an outside package variable through a lexical binding in Perl
It's a long title, but I'm afraid I can't take a single word out without losing the true meaning of the question. I'll give a quick description of what I'm trying to achieve first, then a long rambling on why I want it done this way. Skip the…

JB.
- 40,344
- 12
- 79
- 106
5
votes
1 answer
When is it appropriate to set a request-scoped variable in a JSP?
In my experience, it is rarely/never necessary to set scope="request" on an EL variable.
For example, I have a page that, given an item parameter, constructs a URL specific to that item based on its properties. This page is included in any page that…

Spencer Small
- 91
- 1
- 1
- 5
5
votes
0 answers
How does JS declare variables and functions without lexical scope in a statement block?
var a;
{
function a() {}
a = 60;
console.log('1: ', a);
}
console.log('2: ', a);
var b;
{
b = 60;
function b() {}
console.log('3: ', b);
}
console.log('4: ', b);
The output is:
1: 60
2: f a() {}
3: 60
4: 60
I…

Drake Xiang
- 368
- 1
- 7
- 14