Questions tagged [dynamic-scope]

Dynamic scoping is a method of variable scoping where variables are bound based upon the call stack, instead of where they are defined.

Dynamic scoping is a method for determining variable scope which uses the call-stack to figure out which value an unbound variable (a variable which is defined outside the currently executing function) should have. Dynamically scoped languages will look up the call stack (first the caller, than the caller's caller, and so on) to find unbound variables.


The more popular method of scoping, lexical scoping (alternatively, static scoping), uses the program source code to figure out how to place scopes. For example, consider the following Python code:

def increment_by(x):
    def inner(y):
        return x + y
    return inner

def main():
    add_two = increment_by(2)
    x = 12
    print(add_two(12))

main()

As output, this program will produce the number 14; this is because, when Python sees the unbound name called x, it looks to see how it was defined in the function increment_by, since inner was defined inside increment_by in the program source code. When increment_by was executed to produce the inner function, x was equal to 2, so Python uses the value 2 for the variable x inside inner.


However, the language Emacs Lisp is dynamically scoped, unlike Python - the equivalent code in Elisp is:

(defun increment-by (x)
    ;; This is actually the function called inner, but without a name
    (lambda (y) (+ x y)))

(defun main ()
    (let ((add-two (increment-by 2))
             (x 12))
            (funcall add-two 10)))

(main)

When run inside Emacs, the above code will return the value 22; this is because, when Elisp sees the unbound name called x, it will look to see what value the caller gave x. Since main calls increment-by, Elisp sees that main defined x to be 12, and thus uses the value 12 for x.


Typically, this tag is meant to denote questions about dynamic scoping and how it affects programming in dynamically scoped languages.

83 questions
0
votes
0 answers

Static-scope-What is the output?

I'm strugling to understand what is the output of following code (STATIC SCOPE): program Test: var a: integer; procedure Add(a:integer); begin a:= a+1; end; procedure Print; begin a:= a+2; write(a); end; procedure First; var a: integer; begin a:=3;…
Djole
  • 1
0
votes
1 answer

When invoking an assignment of a method to itself, why is `this` undefined?

This code defines an object bar and calls the function foo in various ways: "use strict"; function foo() { return this; } let bar = { foo, baz() { return this; } }; console.log( foo() ); // undefined (or globalThis outside strict…
Aladdin Mhemed
  • 3,817
  • 9
  • 41
  • 56
0
votes
0 answers

JS this dynamic scoping

When trying to execute the following snippet, I expected the doFoo.call(obj, obj.foo) call to return "inside obj" as the dynamic scope of the previous element in the call stack has a reference to a but the call has the global context instead. What…
PsNeo7
  • 1
0
votes
3 answers

Common Lisp structures with dynamically scoped slots

Common Lisp is lexically scoped, but there is a possibility to create dynamic bindings with (declare (special *var*)). What I need, is a way to create a dynamically scoped structure slot, whose value is visible to all other slots. Here is an…
IgSokolov
  • 55
  • 4
0
votes
2 answers

Difference between Special Variable and Global Variable

In GNU CLISP 2.49.92, the following code: (defvar i 1) (defun g () (format T "g0:~d~%" i) (setf i 2) (format T "g1:~d~%" i)) (defun f () (setf i 3) (format T "f0:~d~%" i) (g) (format T "f1:~d~%" i)) (f) gives the following…
0
votes
1 answer

Dynamic scoping in Javascript similarly to Mongo

I have been trying to wrap my head around Dynamic Scoping, I love that with MongoDB you have the option to scope with strings, for example (this is pseudo and has not been tested) const data = Schema.find({"scope.to.nested":…
0
votes
5 answers

Assigning to a variable in a parent context in Bash

I want to write a function similar to the read built-in, where I pass a variable name as an argument, and the function returns its result into the variable named. I tried doing it like this: #!/bin/bash FIB_CALLS=0 # usage: fib $variable $index #…
user3840170
  • 26,597
  • 4
  • 30
  • 62
0
votes
2 answers

Is there a way to declare that a function should use the scope of the caller?

is there a feautre similar to C macros which lets you reuse code in an inline manner, without creating a seperate scope for that piece of code? for example: a=3 def foo(): a=4 foo() print a will print 3, however i want it to print 4. i am aware…
Archop
  • 37
  • 6
0
votes
1 answer

AngularJS is not updating dynamic scope variable in http post success

I'm trying to get data from database and assign it to a dynamic scope variable with a function but it doesn't assign the data to the dynamic variable at first attempt. Can anyone help? This is my dynamicScope function; $scope.dynamicScope=…
0
votes
1 answer

I don't see why the variables are hidden in that order using dynamic scooping

Using (Dynamic scoping) procedure Main is X, Y, Z : Integer; procedure Suba is A, Y, X : Integer; begin -- Suba body end; procedure Subb is A, B, Z : Integer; begin -- Subb…
complexityyy
  • 123
  • 4
0
votes
2 answers

dynamic scope and `this` in Kyle Simpson's YDKJS

In his (great) series of books “You don't know JS", Kyle Simpson states that dynamic scope and this mecanism are "near cousin", he also says that : "the this mechanism is kind of like dynamic scope." (YDKJS, Scope and Closure, Appendix A) What…
godot
  • 1,550
  • 16
  • 33
0
votes
1 answer

How to manipulate variable scope?

Learning some TypeScript. Trying to make this bit of code work: ... ocrText: string; ... foo() { Tesseract.recognize(document.getElementById('image')) .then(function(result) { console.log(result); …
Eduardo
  • 277
  • 1
  • 6
  • 17
0
votes
0 answers

What will be output of following code in different scoping and calling convention?

int a = 2; void f(int b){ b = b*a; a = a-b; } void main(){ int a = 10; f(a); print a; } a) Call-By-Value and Lexical Scoping b) Call-By-Value and Dynamic Scoping c) Call-By-Reference and Lexical Scoping d) Call-By-Reference and Dynamic Scoping My…
0
votes
1 answer

how to access Dynamic scope returned in angularjs

I have a requirement – single angular js function to return the scope variable dynamically based on one of the input parameter to the function. For controller function, I found some example on how to return dynamic scope like $scope[attributeId] =…
0
votes
0 answers

Dynamic scoping in each evaluation direction

Given the following [arbitrary language, although I think this is ALGOL] program: program main; // A main parent level var i : integer; // A 'global' variable (* Note that all parameters…
chris Frisina
  • 19,086
  • 22
  • 87
  • 167