Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Closure in Java?  RSS feed

 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the http://javac.info/, they say that Java will support "closure".
In my opinion, closure is muck like local/anonymous class, and introducing closure makes language more complex.

Could someone explain about details of closure, and the necessarity of it?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Bupjae Lee]: Could someone explain about details of closure, and the necessarity of it?

Well, you've already given a link to an excellent source of information, with several articles and tutorials. What are you looking for that isn't already covered there? It seems like a waste of people's time to "explain about details" when so many explanations already exist. Do you have more specific questions?
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I want to know about someone's opinion - is Closure necessary for Java? In my opinion, anonymous class is as good as closure, and intoducing closure would make the Java too complex.

If someone has other opinion, i want to hear that opinion.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it necessary? Well, no - you can program in any Turing-complete language, after all. A better question is whether closures are worthwhile, whether they have enough benefits to be worth the added complexity of adding them to the language. Personally I think yes, they are worthwhile. They can do a lot more than anonymous classes can. The fact that they're able to access non-final variables gives them a lot more flexibility, and the fact that their syntax is much shorter than that for anonymous classes means people will be willing to use them in cases where anonymous classes seem clunky. Plus, with anonymous classes you need a preexisting interface or class with a method with the desired parameter types, return type and throws clause. Closures can declare such methods on the fly without having to go back and create a new type first. In short, closures are much more powerful and useful than anonymous classes.

I do have some concerns though. The whole business with nonlocal returns may be more complexity than most people want. Supposedly it makes refactoring easier, since you can wrap a closure around existing code without changing the semantics of return, break or continue. But I think nonlocal returns may be too confusing for some people, and I'm not sure the added complexity is really justified here.

My other concern is more of a regret - I wish closures had been a part of the language from the beginning, rather than an add-on much later. Because then the Java libraries would have been designed with closures in mind, taking full advantage of the mechanism much the way Ruby does. Unfortunately that's not the case, and even after closures are added (assuming they are) we'll still have huge amounts of legacy code that doesn't make effective use of them. And those old methods will probably never be eliminated because they're necessary for backwards compatibility. So for example, people will still have ample opportunity to forget to close a FileOutputStream, because they use it the old-fashioned way rather than using a load pattern method that closes the stream automatically. The benefits of closures would have been much greater had they been part of the language from the beginning, I think.

Still, I think closures are worthwhile, and I hope they make it in to the language.
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. Now I understand why closure is 'worthwhile' for java.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bupjae Lee:
Thanks a lot. Now I understand why closure is 'worthwhile' for java.


That's only one opinion. I would respectfully disagree with Jim.

It's not that I think Closures are not a good programming language feature. It's that I think it is too late for Java to be adopting major new features. I think, if you want features Java doesn't have, go use a language that does. Bolting on new stuff now just leaves a mess - such as the lack of support for the feature in the existing APIs, as Jim says.

That said, I was pleased when Generics arrived in Java...
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:

It's that I think it is too late for Java to be adopting major new features.

That said, I was pleased when Generics arrived in Java...


Interesting contrasts.
I tend to agree that its too late. Generics were added late, and while I use them, some of the syntax is clunky.

The curse of backwards compatibility. I'd hate to see Java growing like C did to become C++, an evil language in my eyes.

But I'm also not sure I agree with 'pick another language'
Systems built with many languages have their own problems, and there is a lot of legacy Java code out there. And I've not found a new, hot language that I like better. Yet.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!