What is Closure in Programing Language (2012)
What is Closure
Closure is a feature of programing language design, for functions. Closure is function that carries a environment for all the variables it uses.
Closure is useful when in a situation where a function f returns a function g, and g uses a variable x defined locally in f. Without closure, the x used by g would be gone out of scope.
- Let's say you define a function f.
- Inside f, you declare local variable x.
- Inside f, you also define a function g (a inner function).
- The function g uses variable x.
- Let's say the function f returns the function g.
- Now, outside f, you call f, it returns g.
- Now, g uses x, but x is inside f, but f is already gone. What to do with x?
- If the programing language is designed in such way that x still exist, we say, the programing language support closure (it properly manages free variable), and g is a closure. Otherwise, the programing language does not support closure.
Example of Closure
Meaning of Closure Outside of Programing Languages
Outside of computer science, “closure” also means:
- In non-programing contexts, it means something is “closed”, or, a sense of completeness. For example: “I'm returning to my hometown for a sense of closure.”.
- In math, algebra, it means a property of a algebraic structure, namely: a function with input from elements of the set, whose result is always also a element in the set. (That is, we say, the set is “closed” under this function) For example, it's common to hear: “the addition operation is closed over integers.”. It means, adding 2 integers results in a integer. Division, for example, is not closed under integers. That is, dividing 2 integers may result in something that is not a integer. For example, fraction.
Closure is Programing Language Design Concept, Not Math Concept, Not Algorithm Concept
Closure, as a feature of computer language design, is not a mathematical concept. It has no point in the science of algorithms.
Closure as Stateful Function, Has the Same Interface as Function Using Global Variable for State
If you consider closure in practice as function with a internal state, and if you consider it as an Interface, as in API, you can see that the so-called “closure” is no different from a function using global variables. No difference in Interface, but just implementation difference.
Global Variable as Alternative to Closure
For example, if you define a global var, and your function access and or set that global var, your function will be able to maintain a state.
However, we all know global vars are bad because, for example, you have this pool where everyone and every function accesses; it's hard to maintain and could be over-ridden by mistake.
So, a crude way to remedy this, is by following a naming convention. For example,
create global variables whose whose name starts with a dollar sign, followed
by the function name, followed by a random string. e.g. $getSession__ttlqtpvz8i.
Now your function getSession can maintain a state, while the
typical problem with using global vars is avoided.
Now, effectively, your function is said to have “closure”. Typically, that word is used when the language provide means to allow function to have a state without the above hackish method. However, it's no more complicated than that. In fact, it is effectively the only means to implement closure. If the language hides such global vars from user, and provide transparent ways when you create or access them inside your function without some naming scheme hack, that's closure.
Closure's Connection to Object Oriented Programing
Once a functional language's function has private state, it is natural to extend the concept to become “object” as in Object Oriented Programing.
When you start to program in a way of closures, that is, a function uses permanent internal variables to maintain states, and you have many functions like that, naturally, you might systematically explore this paradigm. Namely, instead of just one or two permanent internal variables, each of your function uses tens of such permanent internal vars. You might also start to define inner subroutines. Specifically, functions that lives only inside your function. You might apply these inner functions on your internal vars. This is the starting concept of so-called Object Oriented Programing. (For detail, see: What are OOP's Jargons and Complexities.)
Note: with respect to terminology, “closure” is a bad term. It spreads endless confusion and non-understanding. See:
Better Name: Lexical Enclosure
Tom Novelli suggested that “closure” should be named “enclosure”, or the full “lexical enclosure”. Excellent suggestion!
Different Meaning of Closure: Language Feature vs API
when we think of closure, we think of function with state. however, in some tech sense, closure is just a feature of f of the lang, not necessarily having a state. So, emacs lisp calls all lexical scoped function closure, even if it's just a normal function.
Programing Language Design Problem, Conflict of Interest, Closure or Side-Effect
closure (function with state) should be ban'd. Because, it is no longer a function in the math sense. Side effect becomes the point. It's, a abomination. From a lang design feature point of view, closure is watered down form of class in Object Oriented Programing.
in programing language design, you want simplicity principle. e.g. since subroutine take value and return value, it should consider function as value, and able to take function or return function. No exceptions. Simplicity.
But now you got a problem, when some coder write inner f2 that uses outer f1 var and return f2, a closure! thus creating side effect. So, how to resolve this problem?
One naive solution, is to still allow function as input/output, but ban function with side effect. Usually, you can't just ban side effect function. First your language must to be able to detect it. The solution of ban side effect function usually comes in other forms, e.g. Language with typed var.
Ban Nested Closure?
now let's consider nested closure…
when closure is nested, you have realm within a realm.
by the way, did you know, realm is a technical jargon in ECMA-262? #JavaScript
Closure in haskell does not have state
- the effect of closure is to have a state.
- how can haskell have closure but not state?