30

I'm trying to understand closures, but literally every definition of a closure that I can find uses the same cryptic and vague phrase: "closes over".

What's a closure? "Oh, it's a function that closes over another function."

But nowhere can I find a definition of what "closes over" means. Can someone explain what it means for Thing A to "close over" Thing B?

4
  • en.wikipedia.org/wiki/… - gives you something to start with. A closure "closes" open bindings.
    – Blorgbeard
    Jun 8, 2015 at 1:14
  • 3
    LOL, "A closure is something that closes over open bindings." Could that be any less descriptive?!? Really?!? Really?!?!
    – smeeb
    Jun 8, 2015 at 1:57
  • Well you'd have a point if that was a direct quote from anything I said or linked. Here is what I linked to: "a lambda expression whose open bindings (free variables) have been closed by (or bound in) the lexical environment, resulting in a closed expression, or closure".
    – Blorgbeard
    Jun 8, 2015 at 2:04
  • 1
    The word "closed" here derives from mathematical logic... The relevant concepts are "expression", "free variable", "bound variable", and "closed term". See e.g. en.wikipedia.org/wiki/Free_variables_and_bound_variables
    – Nemo
    Jun 8, 2015 at 2:28

2 Answers 2

14

A closure is a pair consisting of a code pointer and an environment pointer. The environment pointer contains all of the free variables of a given function. For example:

fun f(a, b) = 
  let fun g(c, d) = a + b + c + d
  in g end

val g = f(1, 2)
val result = g(3, 4)  (*should be 10*)

The function g contains two free variables: a and b. If you are not familiar with the term free variable, it is a variable that is not defined within the scope of a function. In this context, to close over something, means to remove any occurrences of a free variable from a function. The above example provides good motivation for closures. When the function f returns, we need to be able to remember what the values of a and b are for later. The way this is compiled, is to treat function g as a code pointer and a record containing all the free variables, such as:

 fun g(c, d, env) = env.a + env.b + c + d
 fun f(a, b, env) = (g, {a = a, b = b})
 val (g, gEnv) = f(1, 2)
 val result = g(3, 4, gEnv)

When we apply the function g, we supply the environment that was returned when calling function f. Note that now function g no longer has any occurrences of a variable that is not defined in its scope. We typically call a term that doesn't have any free variables as closed. If you are still unclear, Matt Might has an excellent in depth explanation of closure conversion at http://matt.might.net/articles/closure-conversion/

Same example in Javascript

Before closure conversion

function f(a, b){
    function g(c, d) {
        return a + b + c + d
    }
    return g
}

var g = f(1, 2)
var result = g(3, 4)

After closure conversion:

function g(c, d, env) {
    return env.a + env.b + c + d
}

function f(a, b, env) {
    return [g, {"a": a, "b": b}]
}

var [g, gEnv] = f(1, 2)
var result = g(3, 4, gEnv)
6
  • Shouldn't it be : The function g contains two free variables: a and b. ? You said just after that a free variable is one which wasn't defined in the scope ?
    – Sehnsucht
    Jun 8, 2015 at 7:38
  • Yes, sorry that was a typo. Thanks!
    – Matt
    Jun 8, 2015 at 12:47
  • @NatalieCardot, Inside the body of function g, arguments a and b are neither passed in as arguments nor defined locally. That fits Udacity's definition quite well.
    – Matt
    Jun 19, 2018 at 18:10
  • The explanation is good, but the example is confusing me. I don't know what programming language is being used, is it possible to add similar code examples using JavaScript? fun f(a, b, env) = (g, {a = a, b = b}) in particular, makes no sense to me and is the most confusing. Having a JS equivalent would help ease the language's specifics and make that answer accessible to more people. May 20, 2021 at 11:36
  • In the JS snippet “Before closure conversion”, I would say that the function g closes over the variables a and b. This usage is consistent with the one in How do JavaScript closures work?. Is this answer saying that nothing is being closed over in the snippet “Before closure conversion”, but something is in the snippet “After closure conversion”? Could this please be clarified? Oct 30, 2022 at 11:36
9

From apple documentation

Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.

But what that means?

It means that a closure captures the variables and constants of the context in which it is defined, referred to as closing over those variables and constants.

I hope that helps!

3
  • Could I say "A closure is a function that has no free variable"? Or is it incorrect? From what I gather from your answer, it might be a good definition. May 20, 2021 at 11:38
  • 1
    @Vadorequest I would say a closed function is a function with no free variables. A closure is a function with free variables, that has been given an environment outside the function where those free variables are bound to values, resulting in the function becoming closed over those variables.
    – MetaEd
    May 21, 2021 at 22:45
  • Thank you! Reading matt.might.net/articles/closure-conversion also kinda helped me understand that better. May 22, 2021 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.