16

Ok, this is a bit of a pedantic question but I'd like to make sure I'm understanding the definition correctly. Is the closure moniker used to describe anonymous functions that can lift variables in the local scope (Whether they actually do or not) or only when they do lift variables in local scope?

In other words if an anonymous function has the ability to lift variables in its local scope (Because the language offers that capability) but doesn't, is it still considered a closure?

My understanding is that it is a closure only when local variables are lifted. But an anonymous function that doesn't (Even though it can) is not a closure. So not all anonymous functions are closures but all closures are anonymous functions.

Again, sorry for the pedantry, but these things gnaw at me. :)

2
  • As far as you ask and not teach, you're not pedantic for me ^^ (dictionary.reference.com/browse/pedantic). This question also interest me!
    – Pascal Qyy
    Commented Nov 5, 2010 at 5:23
  • 1
    Another way to look at it is that in a language that has closures, anonymous functions that do not lift local variables are likely still to be represented at run-time the same way real closures are, for the sake of uniformity. But now that you mention it, yes, people tend to use "closure" only when there's some variable capture. Commented Nov 5, 2010 at 5:35

2 Answers 2

32

Assuming you mean within the context of computer science...

A closure is a first class function which captures the lexical bindings of free variables in its defining environment. Once it has captured the lexical bindings the function becomes a closure because it "closes over" those variables.

Note this means closures only exist at run time.

For a function to be a closure is orthogonal to the function being anonymous or named. You can create a language that allows you to define named functions to be closures.

Here is a "named" closure in Python:

def maker():
  count=[0]
  def counter():
    count[0]=count[0]+1
    return count[0]
  return counter
3
  • So if it doesn't capture local variables then its not "closing over" any variables and therefore its not a closure; that makes sense. Good point on only being closures at runtime. But I guess one could speak of it as being a closure in code if it will be a closure at runtime. Might not be ontologically correct but it is convenient to speak of it that way. Thanks for bringing up the point about how the concept of a closure is orthogonal to the concept of an anon function. I didn't even think about that, but that makes sense. Commented Nov 5, 2010 at 6:04
  • 1
    However (to throw in a monkey-wrench) in languages like Python and JavaScript, the containing scopes are bound, not just free variables. For example, consider the use of eval or locals() -- inside a branch, no less! What now? :-)
    – user166390
    Commented Nov 5, 2010 at 6:49
  • 1
    Python doesn't bind the scope. At the time you evaluate the counter definition, it creates a "closure cell" for each captured variable, and stores them in the counter function. maker().__closure__[0].cell_contents is a reference to the list that was bound to count at the time counter was defined. (It's also a bit confusing that Python sometimes uses "closure" to refer to the tuple of cells, rather than the function that holds them, but ignore that.) Meanwhile, maker().__code__.co_freevars[0] is 'c'. That's how it knows that a reference to c is a LOAD_DEREF on cell 0.
    – abarnert
    Commented Aug 2, 2013 at 23:36
6

One great definition of a closure is defined in lua.org:

When a function is written enclosed in another function, it has full access to local variables from the enclosing function; this feature is called lexical scoping. Although that may sound obvious, it is not. Lexical scoping, plus first-class functions, is a powerful concept in a programming language, but few languages support that concept.

Your Answer

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

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