Closure (computer programming): Difference between revisions

Content deleted Content added
Paraphrase relationship between closure and function object
Link to wikipedia page of function objects
Line 121:
 
=== State representation ===
A closure can be used to associate a function with a set of "[[Class (computer programming)|private]]" variables, which persist over several invocations of the function. The [[scope (programming)|scope]] of the variable encompasses only the closed-over function, so it cannot be accessed from other program code. These are analogous to [[private variable]]s in [[object-oriented programming]], and in fact closures are similar to stateful [[function objects]] (or [https://learn.microsoft.com/en-us/cpp/standard-library/function-objects-in-the-stl?view=msvc-170 functors]) with a single call-operator method.
 
In stateful languages, closures can thus be used to implement paradigms for state representation and [[information hiding]], since the closure's upvalues (its closed-over variables) are of indefinite [[variable (programming)#Scope and extent|extent]], so a value established in one invocation remains available in the next. Closures used in this way no longer have [[referential transparency]], and are thus no longer [[pure function]]s; nevertheless, they are commonly used in impure functional languages such as [[Scheme (programming language)|Scheme]].