In general, a closure is a function whose body has some bound variables available in addition to its explicit arguments. It's a common feature of many programming languages including Haskell, ML, Lisp, Scheme, Perl, and Vesta SDL. (Wikipedia has a more complete definition of the term.)
Every function in Vesta SDL is a closure. A function definition captures all variables in the definition context and carries them with it. (Any function argument that has the same name as a variable in the definition context will shadow that variable.) Not all functions make use of variables from their definition context, but any function can.
The term "closure" is used interchangeably with "function" in several places in Vesta SDL and it's documentation. For example, the "_is_closure" primitive function tests whether a value is a function or not.
Here's a simple example of this feature:
1 {
2 x = 1;
3 plus1(y)
4 {
5 // This function "remembers" the value of x at the time it
6 // was defined.
7 return x+y;
8 };
9
10 return [ three = plus1(2) ];
11 }
Here's a slightly more complicated example:
1 {
2 x = 1;
3 plus1(y)
4 {
5 // This function "remembers" that x=1
6 return x+y;
7 };
8
9 // Changing the value of "x" does not affect the value already
10 // captured when we defined "plus1".
11 x = 2;
12 plus2(y)
13 {
14 // This function "remembers" that x=2
15 return x+y;
16 };
17
18 return [ three = plus1(2),
19 four = plus2(2) ];
20 }
This subject is also covered in the Vesta SDL introductory tutorial talk. The specific examples which are concerned with closures are:
/vesta/vestasys.org/examples/sdl_intro/1/examples/scoping1.ves
/vesta/vestasys.org/examples/sdl_intro/1/examples/scoping2.ves
See also: