Closures: Difference between revisions

From Rosetta Code
Content added Content deleted
(New page: Closure is a subprogram passed to another subprogram or specified in a language construct, in a way that it can refer to its declaration context. Closures are further subdivided into: * ''...)
 
No edit summary
Line 3: Line 3:
* '''Upward''', else outward
* '''Upward''', else outward


A closure is downward when neither the subprogram, nor the context it refers, as well as any types they use, do not leave the corresponding scopes upon passing the closure. It is passed '''down''' the scope. A closure is upward otherwise.
A closure is downward when neither the subprogram, nor the context it refers, as well as any types they use, leave the corresponding scopes upon passing the closure. It is passed '''down''' the scope. A closure is upward otherwise.


Implementation of downward closures does not require much efforts from the language designer. Usually it is merely an ability to pass a subprogram or a pointer to it as a parameter to another subprogram. Problems arise when a nested subprogram has to be returned out of containing subprogram while referencing to the context of the latter. This is an upward closure. The problem here is that the context of the containing subprogram left and abandoned is still referenced by the closure. This blows up the language scoping structure. As a result scoping alone cannot more serve for determination of the objects life times.
Implementation of downward closures does not require much efforts from the language designer. Usually it is merely an ability to pass a subprogram or a pointer to it as a parameter to another subprogram. Problems arise when a nested subprogram has to be returned out of containing subprogram while referencing to the context of the latter. This is an upward closure. The problem here is that the context of the containing subprogram left and abandoned is still referenced by the closure. This blows up the language scoping structure. As a result scoping alone cannot more serve for determination of the objects life times.

Revision as of 08:19, 3 June 2008

Closure is a subprogram passed to another subprogram or specified in a language construct, in a way that it can refer to its declaration context. Closures are further subdivided into:

  • Downward, or else inward;
  • Upward, else outward

A closure is downward when neither the subprogram, nor the context it refers, as well as any types they use, leave the corresponding scopes upon passing the closure. It is passed down the scope. A closure is upward otherwise.

Implementation of downward closures does not require much efforts from the language designer. Usually it is merely an ability to pass a subprogram or a pointer to it as a parameter to another subprogram. Problems arise when a nested subprogram has to be returned out of containing subprogram while referencing to the context of the latter. This is an upward closure. The problem here is that the context of the containing subprogram left and abandoned is still referenced by the closure. This blows up the language scoping structure. As a result scoping alone cannot more serve for determination of the objects life times.