Questions tagged [closures]

A function (often anonymous) bound to the referencing environment in its original lexical scope in such a way that it will still have access to that environment (its variables and other references) if executed outside that scope.

A function (often anonymous) bound to the referencing environment in its original lexical scope in such a way that it will still have access to that environment (its variables and other references) if executed outside that scope.

It is the binding between the function and its original scope which distinguishes a closure from a simple anonymous function. Many imperative languages (e.g. Pascal and C) support function pointers, but when the referenced function is executed, it will only have access to its internal variables and to the referencing environment of the scope in which it is executed.

Closures have been closely associated with functional programming since the invention of the Scheme language (in which all lambda functions are closures). While it is possible to construct a functional language without closures (the first versions of LISP did not have them), all modern functional languages currently support them.

Closures are found much less frequently in imperative languages (while Smalltalk had them, Java still does not - though they are promised in version 8). Dynamic languages (e.g Perl, Ruby, JavaScript) have historically been much more forward in this area than their non-dynamic cousins.

49 questions
162
votes
9 answers

What is a closure?

Every now and then I see "closures" being mentioned, and I tried looking it up but Wiki doesn't give an explanation that I understand. Could someone help me out here?
gablin
  • 17,377
  • 22
  • 89
  • 138
41
votes
5 answers

Is a lambda expression something more than an anonymous inner class with a single method?

There is a new hype with the long awaited lambda expressions in Java 8; every 3 day another article appears with them about how cool they are. As far as I have understood a lambda expression is nothing more than an anonymous inner class with a…
Random42
  • 10,370
  • 10
  • 48
  • 65
31
votes
3 answers

What are the benefits and disadvantages in the approaches of C#, Java and Scala to Closures/Lambdas/...?

I wonder what the technical implementation differences between C# and Scala are and how both solutions compare to the implementation ideas and concerns voiced in the email Peek Past lambda by Brian Goetz, sent to the mailing list of Project Lambda…
soc
  • 381
  • 4
  • 10
17
votes
6 answers

Why is closure important for JavaScript?

C#'s lambda expression also has closures but is rarely discussed by the C# communities or books. I see far more JavaScript people and books talk about its closures than they do in the C# world. Why is that?
TomCaps
  • 311
  • 3
  • 6
15
votes
4 answers

Is garbage collection needed for implementing safe closures?

I recently attended an online course on programming languages in which, among other concepts, closures were presented. I write down two examples inspired by this course to give some context before asking my question. The first example is an SML…
Giorgio
  • 19,486
  • 16
  • 84
  • 135
15
votes
1 answer

Does Groovy follow Tennent's Correspondence Principle?

Here's an interesting discussion of Tennent's Correspondence Principle, and a brief description from Neal Gafter: The principle dictates that an expression or statement, when wrapped in a closure and then immediately invoked, ought to have the same…
Armand
  • 6,508
  • 4
  • 37
  • 53
11
votes
4 answers

How will closures in Java impact the Java Community?

It is one of the most talked about features planned for Java: Closures. Many of us have been longing for them. Some of us (including I) have grown a bit impatient and have turned to scripting languages to fill the void. But, once closures have…
Ryan Delucchi
  • 211
  • 1
  • 3
10
votes
3 answers

Are closures with side-effects considered "functional style"?

Many modern programming languages support some concept of closure, i.e. of a piece of code (a block or a function) that Can be treated as a value, and therefore stored in a variable, passed around to different parts of the code, be defined in one…
Giorgio
  • 19,486
  • 16
  • 84
  • 135
9
votes
3 answers

Why is there so much buzz about closures?

Why is there so much buzz about closures among developers? In my career I never intentionally used them, though don't clearly understand what they are. UPD: just to clarify. The question is about why the closure concept became so talky these days.
user1449
8
votes
3 answers

What common programming problems are best solved by using prototypes and closures?

As much as I understand both concepts, I can't see how can I take advantage of JavaScript's closures and prototypes aside from using them for creating instantiable and/or encapsulated class-like blocks (which seems more of a workaround than an asset…
deprecated
  • 3,297
  • 3
  • 20
  • 26
7
votes
4 answers

Are a class's methods a type of closure?

Per MDN A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created.…
tt9
  • 611
  • 7
  • 19
7
votes
1 answer

What is a closure and how is it implemented in Ruby?

In the context of the Ruby programming language, what is a closure and when do you use one? What are the uses for it in Rails?
Michael Durrant
  • 13,101
  • 5
  • 34
  • 60
7
votes
2 answers

When we talk about 'a closure' do we refer to a single variable, or do we refer to all 'closed' variables?

Let's take the following (JavaScript) code that returns a function that closes over variables x and y to illustrate: function test() { var x = Math.random(); var y = Math.random(); var f = function() { console.log(x, y); }; return…
6
votes
3 answers

what are the benefits of closure, primarily for PHP?

I am beginning the process of moving code over to PHP 5.3 and one of the most highly touted features of PHP 5.3 is the ability to use closures. My understanding of closures is that they allow anonymous functions, can be assigned to variable names,…
Patrick
  • 2,922
  • 2
  • 21
  • 24
6
votes
4 answers

javascript closure = c# properties?

For anyone who is expert in both Javascript and C#: Can we think of JS closures the way we think about Private and Public Properties in c#? A closure refers to an inner function (public property) that has access to the parent function's variables…
nanonerd
  • 189
  • 1
  • 6
1
2 3 4