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
5
votes
2 answers
Why does this value not live long enough?
I don't understand why I am getting the following compiler error from this code:
struct Superhero<'a> { name: &'a String, power: &'a i32 } // 1
// 2
fn main() { …

ObliqueMotion
- 483
- 1
- 5
- 11
5
votes
1 answer
Lexical Bindings in Common Lisp Macros
I am currently working my way through Graham's On Lisp and find this particular bit difficult to understand:
Binding. Lexical variables must appear directly in the source code. The
first argument to setq is not evaluated, for example, so…

MadPhysicist
- 5,401
- 11
- 42
- 107
5
votes
3 answers
Understanding lexical scoping - is Wikipedia correct?
I've been trying to grok lexical scoping (I'm far from convinced by the use of the word lexical but that's another discussion) and I've looked at Wikipedia's entry.
According to the fairly simple Bash script example
$ x=1
$ function g () { echo $x…

Edwardo
- 872
- 1
- 11
- 24
5
votes
3 answers
Does lexical scope have a dynamic aspect?
It seems to be a commonplace that accesses to lexical scope can be worked out at compile time (or by a static analyzer, since my example is in Python) based simply on location in the source code.
Here is a very simple example where one function has…

gnarledRoot
- 317
- 2
- 11
5
votes
2 answers
Locally change an attribute of a class in Perl
I have come across an odd problem in one of my Perl scripts. I have a Perl object. Within a certain scope I want one of the objects attributes to be changed, but I want the attribute to be restored to it's old value after it leaves the…

tjwrona1992
- 8,614
- 8
- 35
- 98
5
votes
2 answers
Compiling ES6 arrow functions to Es5 using Babel.js
While looking into ES6 arrow functions' documentation on Mozilla documentation, I got to know that Arrow functions applies all the rules of strict mode, except one as described in the link
var f = () => { 'use strict'; return this};
var g…

Navaneeth
- 2,555
- 1
- 18
- 38
5
votes
2 answers
About lexical scoping in R
I am fairly new to R and while I was reading the manuals I came across a passage about lexical scoping along with this code example:
open.account <- function(total) {
list(
deposit = function(amount) {
if(amount <= 0)
…

Katana
- 103
- 2
- 8
5
votes
1 answer
"Fake" global lexical variables in Common Lisp
It is stated in section "Global variables and constants" of the Google Common Lisp Style Guide that:
"Common Lisp does not have global lexical variables, so a naming convention is used to ensure that globals, which are dynamically bound, never have…

Paulo Tomé
- 1,910
- 3
- 18
- 27
5
votes
3 answers
Emacs: the code in the body of a defun or defmacro cannot refer to surrounding lexical variables?
Update 2013 May: As of GNU Emacs 24.3.1, (let .. (defun..)) bytecompiles just fine without warning and the bytecompiled code works the same as not-compiled code. Just don't forget to add the file variable lexical-binding: t to the file to be…

Jisang Yoo
- 3,670
- 20
- 31
5
votes
6 answers
scheme functions that "remember" values with let/set
I'm new to Scheme and trying to understand how certain values that appear within a function can persist across multiple uses. Take the following counter:
(define count
(let ((next 0))
(lambda ()
(let ((v next))
(set! next (+…

planarian
- 2,047
- 18
- 18
4
votes
2 answers
Why can R not find the value of an argument through lexical scoping?
I have been reading Hadley Wickham's Advanced R in order to gain a better understanding of the mechanisms of R and how it works behind the scene. I have so far enjoyed it and everything is quite clear. There is one question that occupies my mind for…

Anoushiravan R
- 21,622
- 3
- 18
- 41
4
votes
1 answer
Lexical or Dynamic scope - Haskell implemented evaluator
My professor has given us a question after talking about the difference between lexical and dynamic scope. He presented us a simple evaluator (of a fictional language) coded in Haskell. The following is the code:
type Var = ...
type Env = ...
data…

Yan Zhuang
- 436
- 3
- 10
4
votes
1 answer
What is the Lexical Scope in dart?
Basically I am going through the definition of closure functions which says -
A function that can be referenced with access to the variables in
its lexical scope is called a closure
So I want to know this term lexical scope.

B.shruti
- 1,589
- 1
- 21
- 41
4
votes
3 answers
Object method using between lexical scope or this-binding?
I am having trouble determining what concept explains the reason as to why the value of the object's property "count" is retained in the code below.
I have read and reviewed the this and object prototype section from Getify's You Don't Know JS
as…

Kyle Fong
- 131
- 3
- 12
4
votes
3 answers
why is IIFE needed to create a new scope?
From You Don't Know JS:
for (var i=1; i<=5; i++) {
setTimeout( function timer(){
console.log( i );
}, i*1000 );
}
gives
6
6
6
6
6
but using an IIFE like so
for (var i=1; i<=5; i++) {
(function(){
var j = i;
…

user1592772
- 193
- 1
- 8