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
2
votes
2 answers
Something like let in Ruby
I used to write let-like expressions -- with lexical scope.
So I write my own (sad, but it will fail with multiple threads):
# Useful thing for replacing a value of
# variable only for one block of code.
# Maybe such thing already exist, I just not…

Vladimir Gordeev
- 163
- 9
2
votes
4 answers
temporarily overwrite a globally defined function in Scheme let block?
suppose I have the following functions:
(define (g x) (f x))
(define (f x) (+ 1 x))
I would like to temporarily call g with a different f. For example, something like this:
(let ((f (lambda (x) (+ 2 x))))
(g 5))
I would like the code above to…

xdavidliu
- 2,411
- 14
- 33
2
votes
2 answers
In Elisp, how to access the value cell of a symbol that's bound locally from a closure?
As in the code below, I define a function to create a closure that accepts one argument, the value of which is expected to be a symbol referring to a variable bound in the context of this closure. In the body of the closure, I use symbol-value to…

louiet
- 33
- 2
- 6
2
votes
3 answers
Learning JavaScript: Lexical Versus Dynamic Scoping
So I was reading this book, following along with the code examples and running them using node console. In Chapter 7, paragraph 'Lexical Versus Dynamic Scoping', the author claims that the following code will result in an error:
const x =…

John bracciano
- 81
- 5
2
votes
1 answer
Does Async process complete as expected even when component is destroyed
I have a pop up modal, when the user clicks on submit in the modal, it goes to my database and saves some data there. The modal is closed after that.
However, since it is a modal, the user is able to close it via ESC button. So I want to ensure…

ErnieKev
- 2,831
- 5
- 21
- 35
2
votes
1 answer
Can you explain this LISP function and why issues arise with dynamic vs.lexical scoping?
While reading up on some lisp history here From LISP 1 to LISP 1.5, I came across this function:
(define (testr x p f u)
(if (p x)
(f x)
(if (atom? x)
(u)
(testr (cdr x)
p
f
…

firephyz96
- 263
- 2
- 6
2
votes
1 answer
switch off lexical scoping in javascript
Is it possible to change the lexical scoping of javascript so that functions use the variable scope that is in effect when they are evoked not when they were defined? In a nutshell can I change the scope chain associated with a function?
An example…

nickel
- 23
- 5
2
votes
1 answer
Override function value with asynchronous call
How can I use cl-letf or similar to override a symbol's function value during an async call? I want to stop a buffer being displayed after calls to start-process or start-process-shell-command, and instead get back a string instead.
Here is a…

Rorschach
- 31,301
- 5
- 78
- 129
2
votes
1 answer
Why doesn't the variable stay when I am using the setTimeout method?
var obj = {
id: 1,
getId: function(){
console.log(this.id)
}
}
obj.getId(); // 1
setTimeout(obj.getId, 1000); // undefined
So I am trying to understand that calling the method directly is doing well but when I call it using…

Rohan
- 13,308
- 21
- 81
- 154
2
votes
2 answers
What exactly means the lexical scope concept in JavaScript?
I have the following doubt relating the exact meaning of the lexical scope concept in JavaScript.
So, from what I have understood it can be explained by:
void fun()
{
int x = 5;
void fun2()
{
printf("%d", x);
}
}
showing…

AndreaNobili
- 40,955
- 107
- 324
- 596
2
votes
1 answer
Understanding lexical scoping in golang
https://play.golang.org/p/kK9c71Yt9N - This is the code I'm working off of.
I'm trying to understand lexical scoping for the variable X. If I use the := operator in line 11, X defined outside of func main gets hidden and a new scope is getting…

Venkat
- 1,095
- 2
- 13
- 25
2
votes
1 answer
OCaml lexical vs dynamic scoping
I have doubts about some topics. In short I have to write an interpreter in Ocaml both static and dynamic scope.
For now I implemented a version with static scoping through the use of an environment (IDE * value) lists, and a eval (evn * exp),…

Jilz
- 31
- 5
2
votes
1 answer
Variables declared with our and BEGIN block interactions
Why would an uninitialized variable behave/interact differently than an initialized variable in this situation:
use strict;
use warnings;
our($foo) = 0;
BEGIN {
$foo = 2;
}
our($bar);
BEGIN {
$bar = 3;
}
print "FOO:…

Joe Casadonte
- 15,888
- 11
- 45
- 57
2
votes
2 answers
Nested Functions, Closures and Scope
I've been trying to wrap my head around scope, specially closures.
I know that there are many posts about the topic, and I've been reading a lot. But most places refer to the topic as advanced, and use terminology that is relatively difficult to…

Scb
- 339
- 1
- 5
- 19
2
votes
1 answer
lexical-binding not being enabled when evaluating .emacs
Here are some excerpts from my .emacs:
(setq lexical-binding t)
;; .emacs
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only…

Robin Green
- 32,079
- 16
- 104
- 187