Hacker News new | comments | show | ask | jobs | submit login

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.




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.


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.


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.


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]


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.


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?


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




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: