Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
3  
How would you be able to intentionally use something which you don't know what it is? So basically you should first be asking (or searching rather) what closures are. –  Steven Jeuris Jul 6 '11 at 14:15
1  
Did you read the wikipedia article? What did you fail to understand about it? –  kevin cline Jul 6 '11 at 14:18
2  
What is your career based on? –  user1249 Jul 6 '11 at 14:20
4  
What kind of a question is this? What "buzz"? What don't you understand? Why didn't you ask that question instead? –  Aaronaught Jul 6 '11 at 14:24
4  
In defense of the OP, closures are an old concept but only recently have they been the talk of the town, so to speak. And the concept is indeed very simple to anyone who's learned a bit more maths, I also can't understand why people talk about them as if they were something very difficult to comprehend. Maybe that's just a way to make themselves look smarter: "look, I grok closures which are so hard to grok, so I rock". –  quant_dev Jul 6 '11 at 16:42
show 15 more comments

closed as not a real question by Aaronaught, Chris, Larry Coleman, kevin cline, Anna Lear Jul 6 '11 at 21:11

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

up vote 22 down vote accepted

A closure is code that remembers the world where it came from while still being usable where it has been brought to.

An example is defining an anonymous function in Java which knows that it is inside YourObject and can manipulate its methods and functions. This function is then delivered to e.g. Swing where it goes deep inside e.g. a Listener but still has a lifeline back to its roots.

This is a very powerful concept as it allows you to deliver code which - unbeknownst to the code using it - can reach back into other parts of the code.

share|improve this answer
1  
Yes. It's a way to inject not only procedure, but state, into other code. This allows for a very high degree of flexibility in designing algorithms; you can design templated methods which depend on stateful subroutines, which it may not even know about at compile-time. The downside is that the link to that method's original source, and thus state, can be broken without the consuming method knowing about it; when that happens you get hard-to-find runtime errors. –  KeithS Jul 6 '11 at 16:45
1  
Ha ha, anonymous function in Java... Funny. –  configurator Jul 6 '11 at 19:20
1  
@configurator, not funny, a fact. –  user1249 Jul 6 '11 at 19:33
    
stackoverflow.com/questions/2755445/… Java 7 is not out yet, no? –  BlueRaja - Danny Pflughoeft Jul 6 '11 at 20:19
    
java 7 does not have closures. –  user1249 Jul 6 '11 at 20:21
show 5 more comments

Closures are just something to solve a variety of problems in an elegant way. Actually, in programming languages where no closures exist, techniques are created to implement similar functions.

Just think of functors in C++ or Runnables in Java. They are just techniques that allow functionalities similar to closures. Some kind of « manual closures ».

Closures are getting more popular because they are integrated into popular languages : Javascript is growing due to online applications (like google doc for example), C# implemented it, PHP implemented it since 5.3, and so on.

Now that closures are available in more and more technologies, it becomes quite straightforward that more and more people are interested in them.

So now, what are closures ? This is quite simple. A closure is a function and a context to execute it within. This is manipulated as an object. Why is this useful ? This is useful to hook your own code into existing code.

Here are two common situations which require that : when actions are managed by another piece of code, like in multithreading with a thread pool, or when an action has to be executed on a choosen event (used often in javascript for the UI).

share|improve this answer
    
Did I clearly understand you? There are some "manual closures" (not really a closures, but something that resembles their behavior) and there some "TRUE closures"? –  user1449 Jul 6 '11 at 14:35
    
Closure is a language capability. Some language doesn't have that capability, so you have do closure manually using tools that the language provides to you. Saying that they are TRUE or not isn't a very interesting debate IMHO. –  deadalnix Jul 6 '11 at 14:41
2  
@gasan, you can always whip out the assembly and manually perform whatever pattern you want with a bucketful of GOTO statements. Similarly you can manually perform a "for" loop with a "while" loop (in C++). The more advanced, higher levels offer a standardized way of doing common things. –  Philip Jul 6 '11 at 15:05
add comment

I saw an amusing quote the other day, it was along the lines of "classes are data with functions. closures are functions with data".

Yes, it's an oversimplification, but it helps to get the point across.

share|improve this answer
    
+1 Nice way to see what is a closure :D –  deadalnix Jul 7 '11 at 11:55
add comment