• Post Reply Bookmark Topic Watch Topic
  • New Topic

Closures  RSS feed

 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have read that closures are considered a powerful feature that Java lacks. So I went and studied what they are in JavaScript.
Basically they are possible only because functions are first-class citizens and can be returned and stored in references.
But I am yet to figure out what new ways of programming they have given way to.
What can you do with closures that you can't (at least not easily) do in Java?

Thanks.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12156
256
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Closures for Java PDF, page 6, gives a really good example of a common idiom that will become easier with closures:

Current code style:


If closures had been available, these might have been writable as:

Take a look at the PDF for far more examples of many other things you can do with closures.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course if methods are allowed to live outside of objects, you don't have to write a wrapper object to use a method, and code
becomes shorter. Not surprising.
Thanks for the PDF though. I'll have a look.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did a bit more investigation on the subject. It appears to me that there's confusion on what a closures is. Sometime it refers to a local function that accesses a variable from the outer scope, in which case I don't see the need from them in Java or how they can be implemented because it's natural for a member method to access the attributes of the object in which it's declared in. Other times it refers to a function that's passed as an argument to another (a Callback) which is done in Java via passing Interfaces. For example, to define an event handler in JavaScript using jQuery :



and in Java:



The argument to introduce closures would then be succinctness, but this is easily cached up by IDEs code completion features.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marwen Bakkar wrote:I did a bit more investigation on the subject. It appears to me that there's confusion on what a closures is. Sometime it refers to a local function that accesses a variable from the outer scope,

Yes, this is the correct, original meaning of the term.

marwen Bakkar wrote:in which case I don't see the need from them in Java or how they can be implemented because it's natural for a member method to access the attributes of the object in which it's declared in.

But what if the variable is local and non-final? True closures (in languages that have them) would be able to modify those too.

marwen Bakkar wrote:Other times it refers to a function that's passed as an argument to another (a Callback)

Note that in the previous definition, a true closure is usually being passed to some other method as well. It's just that in this newer, weakened definition of "closure", you can't access local variables (unless they're final). These are still useful, but shouldn't be called closures. Better terms are "lambda expression", "lambda", or "function literal".

marwen Bakkar wrote:which is done in Java via passing Interfaces.

As Andrew showed above.

marwen Bakkar wrote:The argument to introduce closures would then be succinctness, but this is easily cached up by IDEs code completion features.

Remember, code needs to be read as well as written. Auto-completion does not clean up overly-verbose code when you need to read it.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:

marwen Bakkar wrote:in which case I don't see the need from them in Java or how they can be implemented because it's natural for a member method to access the attributes of the object in which it's declared in.

But what if the variable is local and non-final? True closures (in languages that have them) would be able to modify those too.


Sorry I'm not following. Local to what? Could you give an example?

So far the only advantage of closures is not having to wrap code inside objects. But wouldn't introducing them violate Java's philosophy where everything is an Object?
Other than that, I don't see anything in closures that has given way to new ways of programming.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marwen Bakkar wrote:Sorry I'm not following. Local to what? Could you give an example?

Local variables are variables defined inside methods, constructors, or initialization blocks. Usually, inside methods. Any variable that is not a member field in a class (static or nonstatic) is a local variable.

marwen Bakkar wrote:But wouldn't introducing them violate Java's philosophy where everything is an Object?

Not really. They exist in several other languages that enforce the "everything is an object" philosophy more strictly than Java does; it's not a big deal. A function can still be an object - it just has a concise syntax that focuses on the important part, the function, without all the other parts that we don't care about.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!