Posts: 135 / Nickname: fsommers / Registered: January 19, 2002 7:24 AM
Considering Closures for Java
December 20, 2006 4:00 PM
|
In this interview, Neal Gafter talks with Bill Venners about the proposal for adding closures to the Java programming language.
http://www.artima.com/lejava/articles/neal_gafter_closures.html What is your opinion of the closures proposal? |
Posts: 5 / Nickname: dewd / Registered: December 18, 2005 9:34 AM
Re: Considering Closures for Java
December 21, 2006 6:52 AM
|
His withLock example reminded of my newly updated DB Connection Pooling in Ruby. I think it's short enough to fit in this forum:
This is still very new (a couple of days only) and can grow in features over time. I did have some ideas of adding something like "withLock" more generic, but in Ruby it seems that that is unnecessary, being better to enjoy the delegation for each different algorithm/library. I hope this helps in giving some perspective to a very unusual subject. |
Posts: 5 / Nickname: dewd / Registered: December 18, 2005 9:34 AM
Re: Considering Closures for Java
December 21, 2006 6:54 AM
|
P.S., I'm referring to the "@pool_semaphore.synchronize{" blocks.
|
Posts: 4 / Nickname: qrczak / Registered: October 22, 2004 11:19 PM
Re: Considering Closures for Java
December 21, 2006 7:42 AM
|
Nice, but several years too late.
|
Posts: 98 / Nickname: achilleas / Registered: February 3, 2005 2:57 AM
Re: Considering Closures for Java
December 21, 2006 2:34 AM
|
Well, good proposal. Closures are necessary in programming. But Java is getting bloated...perhaps a new language should emerge, one that's based on functional programming but with enough freedom to easily do things?
|
Posts: 409 / Nickname: bv / Registered: January 17, 2002 4:28 PM
Re: Considering Closures for Java
December 21, 2006 8:13 AM
|
> Well, good proposal. Closures are necessary in
> programming. But Java is getting bloated...perhaps a new > language should emerge, one that's based on functional > programming but with enough freedom to easily do things? I don't think it's an either-or choice. I was skeptical of adding closures to Java at this late date in the language's history, but was impressed with Neal's presentation at JavaPolis. Even though I did read the first version of their proposed spec, I didn't quite have time to study it enough to grok it deeply. Once I saw his presentation and got to ask him questions afterwords, I quite liked their proposal. I think we should continue to try and improve Java as much as is possible at this point. I know that each thing you add adds bloat, increases surface area, so the benefit of each new feature has to outweigh the cost of added bloat. But the closures proposal I think shows it is still possible to make enhancements where the benefit outweighs the cost. I and a lot of other people will be using Java for a long time to come, and I don't think Java is good enough to freeze it at this point. On the other hand, I also think it would be a good to foster and nourish the growth and acceptance of other languages on the JVM, and let the market decide which ones should "win." Having a stable Java might make it a bit easier for other languages to integrate nicely with Java, but still, on the whole I hope we can do a few more things to Java to ease existing pain points when programming in Java. |
Posts: 9 / Nickname: roybatty / Registered: September 15, 2003 4:57 PM
Re: Considering Closures for Java
December 21, 2006 4:16 AM
|
> Well, good proposal. Closures are necessary in
> programming. But Java is getting bloated...perhaps a new > language should emerge, one that's based on functional > programming but with enough freedom to easily do things? There's always Scala, Nice (not much development going on), and a Haskell-like language called CAL. I was a bit skeptical about retrofitting closures into Java, but after listening and watching the slides of Neal's presentation I've become convinced that it can be done in a clean manner. The problem with "a new language should emerge" idea is where does this new language come from? Ideally, it should have the backing of a Google, IBM, or Sun. I really like the Nice language, but I know that it sitting over at SourceForge pretty much guarantees non-adoption. Scala is in active development, but it lacks good Eclipse integration. That's another show stopper for these alternative langauges on the JVM. It can be the greatest thing since sliced bread, but if it doesn't have good IDE support in this day then forget about it. |
Posts: 409 / Nickname: bv / Registered: January 17, 2002 4:28 PM
Re: Considering Closures for Java
December 21, 2006 8:29 AM
|
> The problem with "a new language should emerge" idea is
> where does this new language come from? Ideally, it > should have the backing of a Google, IBM, or Sun. I > really like the Nice language, but I know that it sitting > over at SourceForge pretty much guarantees non-adoption. > Scala is in active development, but it lacks good > od Eclipse integration. That's another show stopper for > these alternative langauges on the JVM. It can be the > greatest thing since sliced bread, but if it doesn't have > good IDE support in this day then forget about it. > I once heard Geir Magnussen (the guy who did Velocity and is now in charge of Jakarta at Apache) say, "Open source is not about them. It's about us." I'm not sure if he made that up. Sounds like something he might have been quoting of others. But anyway, I've been interested in Scala, and have been worried about its tool support. Not just IDE integration, but also code coverage, style checkers, static analysis tools that find bugs, etc. To compete with Java, you need to have good tool support, because Java has outstanding tool support. But on the other hand, a lot of these tools in the Java arena are open source, and so "we" (the Scala community we) can help improve the situation. If we (the Artima we) decide to use Scala, we will surely try and help with tools to the extent we can afford to do so, because it helps us. I also was told by Eugene Vigdorchik from JetBrains that IntelliJ is working on a Scala plug-in for IDEA (he said this on the Scala mailing list). I have the feeling that those languages that do rise to the top of a free-market competition for alternative languages on the JVM, will get good tool support. The trouble I see is more that Java-based web applications at least, are already chock full of too many languages. You've got to know various flavors of XML, especially HTML, JavaScript, some templating language (such as Velocity, JSP, or near to my heart StringTemplate), Java, maybe Flex for Flash stuff here and there. The prospect of adding another one or two (Groovy, Jython, JRuby, Scala, etc.) makes it even more of a burden on the individual developers to be multi-lingual. But honestly after soaking up the zeitgeist at JavaPolis last week, I'm even more strongly leaning in the direction of picking some other language to for hand-coding for our Java--I mean, JVM--applications. The contest for me is currently between Groovy and Scala. |
Posts: 51 / Nickname: igouy / Registered: July 10, 2003 7:42 AM
Re: Considering Closures for Java
December 21, 2006 9:43 AM
|
Dick Ford wrote Scala is in active development, but it lacks good Eclipse integration.
Their Eclipse plugin is on version 2.4.8, what in particular does it lack? |
Posts: 9 / Nickname: roybatty / Registered: September 15, 2003 4:57 PM
Re: Considering Closures for Java
December 21, 2006 0:52 PM
|
> Dick Ford wrote Scala is in active development, but it
> lacks good Eclipse integration. > > Their Eclipse plugin is on version 2.4.8, what in > particular does it lack? Good is the keyword here, as in what you get for Java. |
Posts: 51 / Nickname: igouy / Registered: July 10, 2003 7:42 AM
Re: Considering Closures for Java
December 21, 2006 3:25 PM
|
An specific example would have been helpful.
|
Posts: 9 / Nickname: roybatty / Registered: September 15, 2003 4:57 PM
Re: Considering Closures for Java
December 21, 2006 4:14 PM
|
> An specific example would have been helpful.
Somehow I doubht that. |
Posts: 409 / Nickname: bv / Registered: January 17, 2002 4:28 PM
Re: Considering Closures for Java
December 21, 2006 6:56 PM
|
> An specific example would have been helpful.
Do you mean code examples of closures? If so, you can see several at Neal Gafter's JavaPolis talk: http://www.bejug.org/confluenceBeJUG/display/PARLEYS/Closures+for+Java |
Posts: 7 / Nickname: pdoubleya / Registered: June 17, 2005 11:28 PM
Re: Considering Closures for Java
December 22, 2006 0:56 AM
|
I'm all for this proposal. The biggest advantage I can see is not that we'll (just) be able to write closures to support, for example, using and freeing resources, but that the Java language can be extended along certain lines without waiting for the fairly long release cycle to approve and produce something new. It should also take some pressure off the compiler writers, in that some features which previously would require a language change can, in some fashion, be accomplished with closures. Neal already pointed this out, I'm just agreeing this is one great aspect of the proposal.
I expect there will be some wacky uses of this, and libraries for specialists, but I'm not really worried about language fragmentation or dialects. For one, those of us who end up using more general-purpose libraries than we write, it's to our advantage if those who write general-purpose libraries can create and extend those libraries with less code and less effort. Also, it may keep the Java tent open, welcoming more people into the community (or encouraging them to stay). I also expect that, as with annotations, we'll see some leading-edge closure libraries coming out of the open-source world, with some being folded back as "standard" closures shipped with JDK 8 and beyond. I am a little concerned about how the syntax will actually read once it's done. With generics, the very simple use-case (avoiding a cast to a variable) is very clear, but in practice I see people struggling with how to write generic methods and classes, and a great deal of frustration with the more complex cases; many times you have to search around to find someone who's run that gauntlet to explain what the heck is going on. I hope we can avoid that with closures. The good news is, with a free-speech javac we can have experimental versions of this sooner rather than later, and find out what it looks like in practice. That's already happening with local variable type inference, and I think it's a great thing. As far as just starting a new Java--I think this calls for a much wider (and deeper) discussion. The number of languages that run, or at some time ran, on the JVM is humbling when you then remember that you've neither heard of, or used, the vast majority of them. My guess is that J6 and the scripting API will help, but I'm still waiting for a general-purpose solution that lets me: // in Java val = new SomeOtherLanguageObject().calculate(...); using a bridge to instantiate and communicate with other languages is not just sub-optimal, it's a dead end for large-scale integration (or many integration points). If I can easily include multiple languages in one project, from the same IDE, I'll be more willing to explore. Last, serious kudos to Neal for taking the heat as the public face of this closures proposal. While there were have been some good criticisms (or at least reasonable critical positions) put forward in the JL discussions, there's also a lot of people who are just plain mean. Neal, we're with you, man. You rock. Don't stop thinking about tomorrow. Patrick |
Posts: 4 / Nickname: alblue / Registered: April 14, 2003 0:07 AM
Re: Considering Closures for Java
December 21, 2006 6:16 AM
|
Note that the definition of 'closures' is not synonmous with anonymous code blocks. Other languages use the term 'lambda expression' to mean an anonymous block of code; there's some strong opinions against confusing the semantic concept of a closure with the syntactical structure used for defining an in-line function.
There's more discussion on this, including input from other language designers and functional systems, hosted on EclipseZone http://www.eclipsezone.com/eclipse/forums/t86911.html Alex. |
Posts: 98 / Nickname: achilleas / Registered: February 3, 2005 2:57 AM
Re: Considering Closures for Java
December 22, 2006 2:33 AM
|
> Note that the definition of 'closures' is not synonmous
> with anonymous code blocks. Other languages use the term > 'lambda expression' to mean an anonymous block of code; > there's some strong opinions against confusing the > semantic concept of a closure with the syntactical > structure used for defining an in-line function. > > There's more discussion on this, including input from > other language designers and functional systems, hosted on > EclipseZone > > http://www.eclipsezone.com/eclipse/forums/t86911.html > > Alex. he he...funny thread. All the hot heads of the PL community gathered together and they can not give a clear definition for one of the basic constructs of their favorite tool. How do they expect the rest of the world to get functional programming? |
Posts: 4 / Nickname: alblue / Registered: April 14, 2003 0:07 AM
Re: Considering Closures for Java
December 23, 2006 2:58 AM
|
That's precisely why using the term 'closure' is a bad idea. Something like an anonymous function is unambiguous, avoids confusion, and doesn't result in disagreements as to what it should mean. In fact, anything else is better than closure :-)
Alex. |
Posts: 1 / Nickname: ired / Registered: December 27, 2006 6:10 AM
Re: Considering Closures for Java
December 27, 2006 0:20 PM
|
My great hope is that folks responsible for the future of Java restrain themselves from adding unnecessary and cumbersome symbols. (i.e., '=>' and things of that nature)
Why can't we just rewrite the closure as such: { (int x, int y) x + y } or { int x, int y { x + y } } or better yet, why not simply simulate the traditional method call, thereby underscoring the fact that closure is essentially a nested method, thus its parameter declaration and scoping should resemble the familiar method syntax. (int x, int y) { x + y } Why introduce all these new symbols? Java is a clean language, adding two-character symbols for something which can be easily expressed using parenthesis or braces is not buying any "character savings", and does not facilitate familiarity for newcomers. Whats with the symbol love? { int x, int y => x + y } In my opinion, its not clean. If one were to use the colon, it might be better, but then again, why not just use the tried and true method declaration syntax? I've posted this to Neil Gafter's blog, but apparently either the moderation system malfunctioned, or this proposal didn't pass the censor. |
Posts: 1 / Nickname: saren / Registered: January 1, 2007 9:07 AM
Re: Considering Closures for Java
January 1, 2007 3:50 PM
|
I have some doubts about the propositions, it looks for me inconsistent.
What will hapen if I define code like that inside my method: void initListeners() throws SomeException { void addListener(ItemSelectable is) { is.addItemListener( { ItemEvent e => if (...) throw new SomeException(); } ) } } The problem is, that closure may be executed after the end of execution of the enclosing method. If the closure will be executed after the method initListeners() will be finished, what will happen to the SomeException thrown from inside the closure? It can not be just thrown to the Swing's API, because it didn't expect the checked exception of this type after calling itemStateChanged method from ItemEventListener interface. Is there any planned way to inform compiler that a specific closure can not throw Exception (or return a value), because it might be executed after the end of execution of enclosing method? I do not see a clear way to enforce that rules by the compiler. |
Posts: 1 / Nickname: gafter / Registered: October 12, 2003 6:56 PM
Re: Considering Closures for Java
February 10, 2007 11:33 PM
|
> Is there any planned way to inform compiler that a
> specific closure can not throw Exception (or return a > value), because it might be executed after the end of > execution of enclosing method? I do not see a clear way to > enforce that rules by the compiler. Yes, there is a way to do that, and there are a couple of examples in the specification that show how it is done. That's the point of the "throws" generic type parameter. |