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
3
votes
2 answers
How to have function as field value in a lua table, when the function is declared at a later point?
I have the following scenario in which the position of code shall not change. How to modify this code to fix the error without moving the function and table variable. I am a Lua newbie, just 4 days
function a()
print("function a");
end
ftable =…

Anish
- 1,490
- 1
- 13
- 13
3
votes
1 answer
Where can we use Variable Scoping and Shadowing in Go?
Some related posts I've found:
go variable scope and shadowing
Golang: variable scope inside if statements
Limit the scope of variables storing error
Also there are many use cases to Variable Scoping and Shadowing.
Any code samples or answers…
user6169399
3
votes
1 answer
Lexical scope of callback function
could someone explain to me why startDate and endDate are not in scope in callback passed to filter function.
var events = [],
eventsDataSource = [],
statusChstatusChanges = [],
statusChangesDataSource = [];
function filterData() {
var…

pawel.podsiadly
- 171
- 2
- 13
3
votes
2 answers
Why hoist variables when there is TDZ
The ES6 standard comes up with Temporal Dead Zones, making a variable reference not possible in any way until the lexical binding is evaluated. So what does variable creation at the time of lexical environment initialisation mean to
The programmer…

sasidhar
- 7,523
- 15
- 49
- 75
3
votes
2 answers
Understanding Scope
I thought I understood scope however, while thinking of a particular code problem I had, I am confused by the following:
var a = {
aFunc: function() {
console.log(x);
}
};
var b = {
bFunc: function() {
var x = c.x;
console.log(x);
…

Kayote
- 14,579
- 25
- 85
- 144
3
votes
2 answers
Variable hoisting examples
Hi I have a snippet of code. I am confused about change of value x1 when I remove non-related part of same code. I searched about it and I came to know that it has to do with hoisting. But the value x1 is still unclear even with hoisting concept for…

amulamul
- 372
- 3
- 14
3
votes
2 answers
Difference between <- and <<-
CASE 1:
rm(list = ls())
foo <- function(x = 6){
set <- function(){
x <- x*x}
set()
x}
foo()
# [1] 6
CASE 2:
rm(list = ls())
foo <- function(x = 6){
set <- function(){
x <<- x*x}
set()
x}
foo()
# [1] 36
I read that <<- operator can be…

cryptomanic
- 5,986
- 3
- 18
- 30
3
votes
2 answers
passing model parameters to R's predict() function robustly
I am trying to use R to fit a linear model and make predictions. My model includes some constant side parameters that are not in the data frame. Here's a simplified version of what I'm doing:
dat <- data.frame(x=1:5,y=3*(1:5))
b <- 1
mdl <-…

Paul
- 3,321
- 1
- 33
- 42
3
votes
1 answer
Is there a scope analyzer for Python?
Given a python file, I want to find out all of the scopes and declarations for the identifiers in the file.
For example, given this file:
import a
from b import xyz
def my_func(g):
print "2"
my_func(0)
a.function_in_a(3)
xyz(4)
I want the…

oxuser
- 1,257
- 2
- 16
- 23
3
votes
2 answers
Are symbols and names different?
Are symbols and names different? On Lisp by Paul Graham, which focuses on common lisp, has some discussions that seem to imply so, e.g.
Since lambda-expressions are also names of functions, they can also appear first in function calls:
((lambda…

djechlin
- 59,258
- 35
- 162
- 290
3
votes
1 answer
Lexical Scoping in javascript function, why is the code returning undefined
var variable = "top level " ;
function outer(){
alert(variable); // why does this alert returns undefined ??
var variable = " inside outer, outside inner";
function inner(){
alert(variable);
}
…

voodoo
- 31
- 1
3
votes
2 answers
Could anyone please explain this behavior of function inside another function?
I was trying to understand lexical scoping in Javascript. In the below example, I have written a function inside an another function. When I run this, I would expect to pop-up "dad" in the first alert and "mom" in the second alert. But what actually…

himsag
- 417
- 3
- 10
3
votes
0 answers
Python: Enforce local scope using a *constant* closure
Suppose I want to reuse some code, name it "function C" whose usage falls only under the scope of "function R". Nesting the function definition within R, serves to restrict its name in the local scope (the simplest possible closure, if it can be…

digenishjkl
- 154
- 1
- 12
3
votes
1 answer
How are the "lexical and special variable" semantics implemented under the hood in general?
CLtL2 has clarified the distinction between scope and extent. My take on it, in relation to lexical and special variables, is that lexical variables are “lexically scoped with indefinite extent” while special variables are “indefinitely scoped with…

Wei Peng 彭巍
- 99
- 3
3
votes
3 answers
Subtlety about Common Lisp scoping (dynamic vs lexical)
After reading documentation about the declaration SPECIAL, the special operator LET, the macro DEFVAR, and several questions here at StackOverflow about the dynamic versus lexical scoping in Common Lisp, as, for instance, this, I still can't…

Paulo Tomé
- 1,910
- 3
- 18
- 27