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
844
votes
22 answers
183
votes
7 answers
How do you use "<<-" (scoping assignment) in R?
I just finished reading about scoping in the R intro, and am very curious about the <<- assignment.
The manual showed one (very interesting) example for <<-, which I feel I understood. What I am still missing is the context of when this can be…

Tal Galili
- 24,605
- 44
- 129
- 187
49
votes
5 answers
Dynamic and Lexical variables in Common Lisp
I am reading the book 'Practical Common Lisp' by Peter Seibel.
In Chapter 6, "Variables" sections
"Lexical Variables and Closures" and "Dynamic, a.k.a. Special, Variables".
http://www.gigamonkeys.com/book/variables.html
My problem is that the…

George
- 15,241
- 22
- 66
- 83
46
votes
2 answers
What is the meaning of the dollar sign "$" in R function()?
Through learning R, I just came across the following code explained here.
open.account <- function(total) {
list(
deposit = function(amount) {
if(amount <= 0)
stop("Deposits must be positive!\n")
total <<- total + amount
…

Daniel
- 1,202
- 2
- 16
- 25
45
votes
2 answers
Referencing "this" inside setInterval/setTimeout within object prototype methods
Normally I'd assign an alternative "self" reference when referring to "this" within setInterval. Is it possible to accomplish something similar within the context of a prototype method? The following code errors.
function Foo() {}
Foo.prototype = {
…

Huck
- 507
- 1
- 4
- 6
34
votes
4 answers
Ruby - Lexical scope vs Inheritance
This is a continuation this original SO question: Using "::" instead of "module ..." for Ruby namespacing
In the original SO question, here is the scenario presented which I'm still having trouble understanding:
FOO = 123
module Foo
FOO =…

wmock
- 5,382
- 4
- 40
- 62
32
votes
10 answers
What are the advantages of dynamic scoping?
I've learned that static scoping is the only sane way to do things, and that dynamic scoping is the tool of the devil, and results only from poor implementations of interpreters/compilers.
Then I saw this snippet from a Common Lisp vs. Scheme…

Claudiu
- 224,032
- 165
- 485
- 680
18
votes
2 answers
What are the new rules for variable scoping in Emacs 24?
Emacs 24 now has lexically-scoped variables. It also still has dynamically-scoped variables, of course. Now that it has both, I'm quite confused about when a variable will have which kind of scope. There's a lexical-binding variable that controls…

Ryan C. Thompson
- 40,856
- 28
- 97
- 159
17
votes
2 answers
Type extensions and members visiblity in F#
F# has feature called "Type extension" that gives a developer ability to extend existing types.
There is two types of extensions: intrinsic extension and optional extension. First one is similar to partial types in C# and second one is something…

Sergey Teplyakov
- 11,477
- 34
- 49
15
votes
6 answers
JavaScript example question: lexical scoping/closure - Eloquent Javascript
So I'm new to programming and I'm trying to learn JS with the book Eloquent Javascript.
So far so good, until I reached an example with the following code
function makeAddFunction(amount) {
function add(number) {
return number + amount;
}
…

jsebcortes
- 189
- 9
13
votes
2 answers
ES6 arrow function lexical this in V8
I have the following ES6 code using a fat arrow function:
var test = {
firstname: 'David',
fn: function() {
return ['one', 'two', 'tree'].map(() => this.firstname)
}
}
console.log(test.fn())
According to how arrow functions are supposed…

Daff
- 43,734
- 9
- 106
- 120
13
votes
5 answers
How is Lexical Scoping implemented?
A couple of years ago I started writing an interpreter for a little Domain Specific Language which included programmer-defined functions.
At first I implemented variable scope using a simple stack of symbol-tables. But now I want to move to proper…

interstar
- 26,048
- 36
- 112
- 180
13
votes
3 answers
Lexical scope in Emacs: compatibility with older Emacsen
Emacs 24 added optional lexical bindings for local variables. I would like to use this functionality in my module, while maintaining compatibility with XEmacs and the previous Emacs versions.
Before Emacs 24, the easiest way to get closures was by…

user4815162342
- 141,790
- 18
- 296
- 355
12
votes
1 answer
Perl scoping and the life of local variables
How long does the memory location allocated by a local variable in Perl live for (both for arrays, hashes and scalars)? For instance:
sub routine
{
my $foo = "bar";
return \$foo;
}
Can you still access the string "bar" in memory after…

rubixibuc
- 7,111
- 18
- 59
- 98
12
votes
1 answer
What is the reason for the warning that a lexical variable is "not available" within eval
When an eval statement is within the scope of a lexical variable, that variable should be within the lexical context of the evaluated block. Moreover, lexical variables should be available in the lexical context of subs.
Yet this doesn't work:
use…

alexchandel
- 532
- 6
- 15