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.
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.
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.
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).
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.