2
$\begingroup$

If I am correct,

  • The closure of a function is the non-local environment of the function.

  • the non-local environment of a function is different depending on static or dynamic scoping.

So is the closure of a function also different depending on static or dynamic scoping?

Thanks.

$\endgroup$
4
$\begingroup$

I'd view it this way:

  • In dynamic scoping, functions only ever access the local environment
  • In lexical scoping, functions access the environment in which they are created. Because this is non-local to their calling environment, it must be stored in a closure.

Closures really aren't a thing in dynamic scoping: instead of looking for variables in the closure, you look for variables in the environment in which the function was called.

The process of closing over (storing) the environment is what makes static scoping static scoping.

$\endgroup$
  • 1
    $\begingroup$ It looks very dangerous, but theoretically you can capture current dynamic scope in the closure creation context, and then when closure is called, it will resolve variables in this context which may be completely different tree than the currently executed (caller) context $\endgroup$ – Bulat Jun 30 '18 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.