5 ms·
As I understand these terms: A scope is the duration (in code, not time) during which an identifier (name) is bound to a value (thus becoming a variable). When
by dkersten 16y ago
As I understand these terms:
A scope is the duration (in code, not time) during which an identifier (name) is bound to a value (thus becoming a variable). When a variable is said to "go out of scope", what is meant is that the execution leaves the scope in which the name is bound, rendering it invalid (which is why, eg, in C++ automatic (local/stack) variables get destructed when execution leaves their scope - it is invalid and can no longer be accessed.
C++:
{ // In C++ we can create arbitrary "scopes"
int a = 5; // Bind the name 'a' to some variable
...
} // 'a' is not accessible after here, because the scope is terminated by the }
Clojure:
(let [a 10] ; Bind the identifier 'a' to the value '10'
; 'a' can be accessed here
...) ; This ')' closes the scope, so 'a' is no longer accessible
An environment is a special kind of scope that includes functions, variables, classes etc. In some languages this distinction is more important than others (ie, languages with first class functions, normal function-level scopes may contain function definition, whereas languages like C++ do not allow this). I think generally an environment can be thought of as a table of functions and values (usually named, eg, think of shell environment variables) which can be accessed in the current execution context (what is currently being executed). A language may define an environment to have different things, such as I/O functions, state etc, or a language may not have a notion of environment outside of a global collection of names/identifiers bound to something.
A closure is a function (anonymous or named) which "closes over" (captures) the variables in the environment or scope in which it is defined, essentially capturing those variables and/or values. The distinction between variable and value is important here. Eg, Python captures the variable, so if the variable is assigned a different value between when the closure captures it and when the closure is evaluated (called as a function), the closure will see the new value. If it were instead to capture the value, then the changes will not be visible. Java, for example, only allows final (constant) variables to be used in locally defined classes - Java captures the variable but enforces value capture semantics by forcing the variables to be constant.
A continuation is a representation of the programs execution. You could think of a continuation as a jmp_buf used with C's longjmp and setjmp functions. The current continuation is a representation of the next point of execution.
For example:
function foo (a, continuation){
// do something with a
// call continuation
continuation();
}
...
1: call_with_current_continuation(foo, 10); // call/cc function foo
1:
Line 1 calls the function foo, passing the current continuation as the last argument. foo calls the continuation at the end, which essentially returns program execution to the continuation, which is line 2. Used in this way, a continuation is the same as a return statement or function. Of course, you can use continuations to jump anywhere, not just the caller. For example, you can use continuations to implement coroutines or exception handling.