62

Are lambda expressions (and to a degree, anonymous functions) closures?

My understanding of closures are that they are functions that are treated as objects, which seems to be an accurate representation of what anonymous functions and Lambda expressions do.

And is it correct to call them closures? I understand that closures came about (or became popular) due to the lisp dialect, but is it also a general programming term?

Thanks for any clarification that you can provide!

3
  • 6
    NB they're called closures. "Clojure" is a language (hence my edit). Commented Mar 6, 2012 at 20:41
  • Ah - that definitely clears up that confusion! Thanks
    – Stefan H
    Commented Mar 6, 2012 at 20:42
  • Closures are an aspect of lambda expressions. Lambdas need not necessarily support closures. Some languages implement it differently. For eg, Java is different from C# in that former doesn't allow modification of closed over variable within the function. That said I think this question is about general understanding, hence duplicate of What is the difference between a 'closure' and a 'lambda'?
    – nawfal
    Commented Jul 5, 2014 at 12:34

4 Answers 4

121

A lambda may be implemented using a closure, but it is not itself necessarily a closure.

A closure is "a function together with a referencing environment for the non-local variables of that function.".

When you make a lambda expression that uses variables defined outside of the method, then the lambda must be implemented using a closure. For example:

int i = 42;

Action lambda = () => { Console.WriteLine(i); }; 

In this case, the compiler generated method must have access to the variable (i) defined in a completely different scope. In order for this to work, the method it generates is a "function together with the referencing environment" - basically, it's creating a "closure" to retrieve access to the variable.

However, this lambda:

Action lambda2 = () => { Console.WriteLine("Foo"); }

does not rely on any "referencing environment", since it's a fully contained method. In this case, the compiler generates a normal static method, and there is no closure involved at all.

In both cases, the lambda is creating a delegate ("function object"), but it's only creating a closure in the first case, as the lambda doesn't necessarily need to "capture" the referencing environment in all cases.

12
  • 3
    var lambda2 = () => { Console.WriteLine("Foo"); } Does not rely on any "referencing environment", since it's a fully contained method. It closes over the IO system! :) Commented Mar 6, 2012 at 20:52
  • @DanielEarwicker I wouldn't consider a .NET reference as closing over the system. Your setting up the operational environment in which the function operates, not creating an environment specifically for the function itself (which is passed with it). There is a difference. Commented Mar 6, 2012 at 20:59
  • 1
    It was only a half-serious comment - yes, there is no need for the C# compiler to generate a special class to represent a closure. But in general terms a closure is any situation where a function that is dependent on some value that is not passed in a formal parameter. In that sense, even a simple C program, with a function referring to a global variable, is an example of closure. Commented Mar 6, 2012 at 21:18
  • 2
    Note that you cannot use "var" with lambdas in C# because lambdas are typeless. Commented Mar 6, 2012 at 21:21
  • 2
    I'd look at the second example as a degenerate case, but still a closure. Your definition says a closure is "a function together with a referencing environment for the non-local variables of that function.". In this example, there are no non-local variables, so as an implementation detail no environment information needs to be packed up.
    – kvb
    Commented Mar 7, 2012 at 1:45
84

Reed's answer is correct; I would just add few additional details:

  • lambda expressions and anonymous methods both have closure semantics; that is, they "capture" their outer variables and extend the lifetimes of those variables.

  • anonymous function is the term we use when we mean a lambda expression or an anonymous method. Yes, that is confusing. Sorry. It was the best we could come up with.

  • a function that can be treated as an object is just a delegate. What makes a lambda a closure is that it captures its outer variables.

  • lambda expressions converted to expression trees also have closure semantics, interestingly enough. And implementing that correctly was a pain in the neck, I tell you!

  • "this" is considered an "outer variable" for the purpose of creating a closure even though "this" is not a variable.

2
  • 1
    While this isn't actually a variable, I don't think it's too surprising that it would be treated as one for the purposes of closure semantics. It behaves very similarly to how a read-only "variable" would behave. As well, I think it's intuitively part of the context that a closure is intended to capture. Finally, it's one instance where the false (though common, it seems) assumption that closures capture values is not too damaging: this certainly isn't going to changed after the fact!
    – dlev
    Commented Mar 6, 2012 at 21:42
  • 6
    @dlev: it is unsurprising for "this" in a reference type. In a value type, sometimes people take advantage of the fact that "this" is logically an alias to the variable that contains the value of the struct; closing over "this" when in a method of a mutable value type can produce unexpected results. Commented Mar 6, 2012 at 23:36
13

It's "closure" not "clojure."

That is not what a closure is. A closure is basically a representation of a function together with any non-local variables that the function consumes.

In that sense, lambdas are not closures, but they do cause closures to be generated by the compiler if they close over any variables.

If you use ILDASM on an assembly that contains a lambda that closes over some variables, you will see in that assembly a compiler generated class that repsresents the function and those variables that were closed over. That is the closure.

When you say

functions that are treated as objects,

that's normally just "function object" (in C# we'd say "delegate") and is common in functional programming.

11

Yes. Closures typically capture variables from the outer scope. Lambdas can do that. However if your lambda does not capture anything, it is not a closure.

5
  • By this definition would a lambda taking no external variables be a closure?
    – Yuck
    Commented Mar 6, 2012 at 20:42
  • 2
    Lambdas may do that. Jason's answer is more accurate. Commented Mar 6, 2012 at 20:42
  • The term "closure" might be well defined, but it is used loosely in practice. I don't think it can be definitely said that lambdas which capture nothing are not closures.
    – usr
    Commented Mar 6, 2012 at 20:44
  • 3
    @usr Probably used loosely because its definition is not well understood. So, it's really just being used incorrectly.
    – Yuck
    Commented Mar 6, 2012 at 20:45
  • @usr - A good example of misuse of the term was the early stages (the first five years!) of discussion of adding lambdas to Java. The new syntax was referred to as the "closure" feature. More recently they've switched to calling it lambda, in acknowledgement that it's the right term to use. It may or may not involve closure depending on what the lambda body says. Commented Mar 6, 2012 at 20:50

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.