News for hackers. Stuff that matters.
Re:Java 7: Oracle pushes a first version of closures by strebler on Monday May 31, 2010 @11:20AM (#1392670)
Look at what the cat dragged in - a syntax for closures in Java.
Re:Java 7: Oracle pushes a first version of closures by sigzero on Monday May 31, 2010 @11:34AM (#1392701)
Maybe they are trying to make Scala the default language on the JVM with that? :-)
Re:Java 7: Oracle pushes a first version of closures by RodgerTheGreat on Monday May 31, 2010 @11:33AM (#1392698)
We've been calling them "anonymous classes" for years.
Re:Java 7: Oracle pushes a first version of closures by jimbokun on Monday May 31, 2010 @01:36PM (#1393015)
And we've been needing a usable syntax for anonymous classes for years.

I am fully onboard with closures as semantically equivalent to an anonymous class with a single method. The current syntax for creating them, though, is ridiculous.

Re:Java 7: Oracle pushes a first version of closures by RodgerTheGreat on Monday May 31, 2010 @01:52PM (#1393055)
I can see that, but I'd be a lot happier if the new syntax they came up with was more consistent with the rest of the language.

I don't think the current syntax for anonymous classes is 'ridiculous', it's just inconveniently verbose when you only want a single method. If you're implementing a more complex interface, the current method is actually pretty good.

Re:Java 7: Oracle pushes a first version of closures by rictic on Monday May 31, 2010 @04:01PM (#1393341)
It is ridiculous.

Java often completely ignores the design guideline of making the easy things easy and the hard things possible, preferring just to make the hard things possible. Anonymous inner classes with many methods are far less commonly needed than anonymous functions, and it's rare that the program is better served having them inline rather than defined elsewhere anyways.

Five lines and dozens of characters of boilerplate for every anonymous function does come at a cost and is ridiculous.

Re:Java 7: Oracle pushes a first version of closures by orangecat on Tuesday June 01, 2010 @12:55AM (#1394364)
Java often completely ignores the design guideline of making the easy things easy and the hard things possible, preferring just to make the hard things possible.

That's it exactly. My "favorite" example is file I/O. Yes, it's great that I can chain a bunch of InputStream instances together to read encrypted gzipped serialized objects over the network, but it shouldn't be more than one line for the far more common case of reading a file's contents into a string or a byte array.

Re:Java 7: Oracle pushes a first version of closures by bfung on Monday May 31, 2010 @07:30PM (#1393786)
Apologies: was editing, deleted, but people already replied; I always forget the markdown syntax...

Just to play devil's advocate, what is unusable about the syntax for anonymous classes in Java? An example close to bnoordhuis's post below, in today's syntax:

  Collections.sort(list, new Comparator<Integer>() {
    public int compare(Integer i1, Integer i2) {
      return i1.compareTo(i2);
    }
  });
How are closures going to help?
[deleted]
Re:Java 7: Oracle pushes a first version of closures by viraptor on Monday May 31, 2010 @07:26PM (#1393781)
There's nothing unusable about it (by definition). But wouldn't:

    \(Integer i1, Integer i2) { return i1.compareTo(i2); }
be nicer? (or whatever else syntax) Redundancy is already pretty common in Java and the current syntax just seems to try to win the price for most redundancy. You're implementing one interface, which you can infer in most cases with one function which you already know, which returns one result you already know the type of. So why would I want to repeat that?

Sure - you can use the verbose syntax when you have many methods, but this example could be cut down to one line. Less line noise -> less errors.

Re:Java 7: Oracle pushes a first version of closures by DrJokepu on Monday May 31, 2010 @07:23PM (#1393774)
Compare that to how you would do it in C# (note that's not how you do comparisons specifically, this is just to demonstrate the C# lambda syntax):

  list.Sort((i1, i2) => i1.CompareTo(i2));
Isn't that an awful lot more readable / usable?
Re:Java 7: Oracle pushes a first version of closures by bfung on Monday May 31, 2010 @07:34PM (#1393794)
Yes, actually, while I was editing, I was going to mention C# delegates and new lambdas (I've been using C# 2.0, haven't upgraded yet). In my opinion, C# has been the better Java for a while; I think they should just copy C# stuff since it's already been proven to work and has some nice properties/syntax. It's not like C# didn't copy from Java in the earlier days...
Re:Java 7: Oracle pushes a first version of closures by whakojacko on Monday May 31, 2010 @11:33AM (#1392699)
yea, that looks about right.

Everyone looking for these kind of features should probably just be using Scala...

Re:Java 7: Oracle pushes a first version of closures by ericlavigne on Monday May 31, 2010 @12:11PM (#1392813)
While I would prefer Scala over [Java + closures], and would prefer Clojure over either of those, I am still pleased to see closures in Java for several reasons.

1) For now, I still have a job as a Java programmer. Closures in Java will save me some lines of code until I can fix that.

2) Currently each JVM language has its own incompatible reinvention of closures. I think that closures in Java are a step towards better interoperability between JVM languages.

3) A lot of programmers will be introduced to a higher level of abstraction than they otherwise would have.

Re:Java 7: Oracle pushes a first version of closures by technomancy on Monday May 31, 2010 @12:51PM (#1392910)
> 2) Currently each JVM language has its own incompatible reinvention of closures.

Yeah, but as long as they expect Callables in the right place interop can be pretty easy with just interfaces. For instance, you can pass JRuby lambdas to Clojure functions just fine. Sure there's a duplication of effort, but it's not too bad for interop purposes.

Re:Java 7: Oracle pushes a first version of closures by sreque on Monday May 31, 2010 @05:54PM (#1393592)
How in the world do callables replace closures? Closures can have arguments, callables can't. You cannot pass a ruby lambda expecting arguments to Clojure.
Re:Java 7: Oracle pushes a first version of closures by technomancy on Thursday June 03, 2010 @01:25PM (#1401897)
> You cannot pass a ruby lambda expecting arguments to Clojure.

I beg to differ: http://github.com/technomancy/clojure-gem/blob/master/test/t...

Re:Java 7: Oracle pushes a first version of closures by sreque on Saturday June 05, 2010 @02:52PM (#1407255)
Have you looked at the source code for that project? It doesn't use callables at all. the orig_commute function, the original commute method on class Ref, gets passed a monkey-patched ruby Proc object that implements the invoke method which takes one argument, a list, containing all of the original ruby arguments. Clojure then most likely calls invoke inside orig_commute through reflection. The end result: a bunch of hackery specific to Ruby and Clojure, something JVM-level closures would help eliminate.

Again, you cannot pass a ruby function expecting arguments to Clojure! What your link shows is that you can, however, send monkey-patched versions of ruby functions that implement clojure-specific calling conventions to clojure. :)

Re:Java 7: Oracle pushes a first version of closures by pivo on Monday May 31, 2010 @01:56PM (#1393065)
I noticed in the test code linked to by the article that the closed over variables are all declared final. I'm not sure if that's just because of the early stage of implementation or if that's actually going to be a requirement. If so that seems unfortunate and maybe an impediment to interoperability.
Re:Java 7: Oracle pushes a first version of closures by ericlavigne on Monday May 31, 2010 @07:30PM (#1393787)
Closing only over final variables is deliberately a requirement because closing over non-final variables can lead to confusing situations. I discussed this in more detail in a previous thread:

http://news.ycombinator.com/item?id=1257599

Good point regarding the effect on interoperability. I don't have an answer for that.

Re:Java 7: Oracle pushes a first version of closures by sigzero on Monday May 31, 2010 @12:27PM (#1392860)
Re:Java 7: Oracle pushes a first version of closures by radu_floricica on Monday May 31, 2010 @01:52PM (#1393054)
Look, I'm an independent programmer and I can pretty much chose everything about what I program in. Still, when it comes to completely new, and not very mission critical projects, I go for Clojure (still a bit green and unsure on my lipspy feet). When it comes to existing projects and serious stuff, Java is and Java will stay. Way too much code invested in it. I can and I do try to evolve my style and tools, but the one single thing that's always been in my way was a complete lack of higher order functions (anonymous classes are a verbose joke). So yeah, I'm ecstatic :)
Re:Java 7: Oracle pushes a first version of closures by zmmmmm on Monday May 31, 2010 @07:35PM (#1393799)
Even if you do your project in Scala it is tremendously likely that you will be doing so on the back of a ton of Java infrastructure and you will have to interact with it intimately at times. If Java itself gets closures then (in time) we will get support for them in all the base Java APIs and 3rd party libraries and that will make them significantly more pleasant to use for everyone that is building on the JVM.