Java finally getting closures, method handles and traits in JDK 8
javarants.com
javarants.com
Bottom line: Java seems dated.
Now, to my rant.
import java.util.*; interface Sortable<T, U extends Comparable<? super U>> extends List<T> { void sortBy(Extractor<? super T, ? extends U> e) default Impl.sortBy; static class Impl { public static<T, U extends Comparable<? super U>> void sortBy(Sortable<T, U> sortable, final Extractor<? super T, ? extends U> e) { Collections.sort(sortable, #{T a, T b -> e.extract(a).compareTo(e.extract(b))}); } } }
I HATE this code. This is an example of when Java went downhill to me and started to get really, really ugly. I always thought Java was verbose, but with generics and others things it started to get ugly. That was enough for me. I knew Python and Ruby and decided to go a different way. Found Erlang and Clojure started to mature on the JVM. Bye bye Java.
Map<String, Map<String, Map<String, Map<String, String>>> > meta_data = new LinkedHashMap<String, Map<String, Map<String, Map<String, String>>> >();
....
for (Map.Entry<String, Map<String, Map<String, Map<String, String>>> > schema : meta_data.entrySet()) {
I really would rather just type: meta_data = {}
....
for schema in meta_data.iteritems():EDIT: link to better article
new Ext.Panel({
width: 400,
height: 400,
title: 'Pie Chart with Legend - Favorite Season',
renderTo: 'container',
items: {
store: store,
xtype: 'piechart',
dataField: 'total',
categoryField: 'season',
//extra styles get applied to the chart defaults
extraStyle:
{
legend:
{
display: 'bottom',
padding: 5,
font:
{
family: 'Tahoma',
size: 13
}
}
}
}
});You're example Java declaration doesn't seem to actually capture the Javascript code you've got there - ie each initial string does not map to another Map - in some cases it just maps to an integer or string.
By the way - I totally agree that the declarations of stuff in Java with generics is way too verbose.
I've been programming Java since the Alpha days and it is getting to the point where all the extended syntax is a bit outrageous.
The real problem is that most of these extensions are required to make Java more powerful, certainly generics / templating was a huge boon. But examples like the above make my eyes hurt.
I suppose my question is whether you can add all the features that make functional languages powerful while also retaining static typing. That's what Java is trying to do. In same cases the type safety is a huge boon to building a large system with many people working on it, in others something more dynamic allows you to be far more productive. I'm not sure you can have both and still have code anybody wants to write.
I haven't played much with Clojure yet, not sure if that is the best of both worlds.
Of course you can. Look at Scala. (And on other platforms, F#, ML, and Haskell.)
How would you 'clean up' generics syntax in Java?
I suspect the author is confusing closures (a language implementation technique) with anonymous functions (a language feature which can be implemented using closures).
Keep in mind that the concept of a closure has been bred in the functional programming community, where variables aren't mutable.
If you really need to reference a mutable variable in the enclosing scope, you can do in Java exactly what you can do in languages like ML/Scheme -- let the variable reference a mutable box (in Java, an object). The reference to the box (or object) is copied, not the box itself.
In fact, in Java you can go one step further and just reference the entire enclosing scope by storing the parent's "this" in a final variable.
* I make my comments as a Java veteran, and fan of the platform.
Another question as I think they were unrelated. What kinds of features would you like to see in Java that would necessarily break backwards source compatibility? For example, I would like to see an 'auto' or 'var' keyword that is equivalent to the C++/Scala versions of same. Not a huge deal but would be nice.
Thanks!
If my understanding is correct, which shop is going to allow people to start using these new techniques, when they already have a code base full of workarounds? Unless someone is going to sit down and refactor their humun-gi-normous code base, they will in effect be creating a second dialect of Java in their shop if they start using traits and closures instead of whatever they were already doing with Java's existing feature set.
I just can't see this having any more than a token impact. Now Java users can say, "Sure we have closures. We don't use them here, but they exist and if we wanted to, we could use them. But we don't need them, so it's no big deal."
I mean, couldn't your point be made about any language that introduces new features? When that happens, do developers decide to rewrite the entire codebase? I don't think so.
For example, a recent requirement of mine was to revise some xml processing to create sitemaps. As part of the changes, I rewrote the logic to use xml LINQ (c#) and the resulting function is much cleaner, and will be easier to maintain going forward.
It also makes routine maintenance changes more palatable.
As an aside, I think that the complexity of the language is already getting out of hand. Although features like this can be found in "little" languages like Javascript, they were in from the beginning and form part of the language's substrate. Libraries, frameworks, everything a Javascript user touches will make use of anonymous functions with lexical scope. More complex features are built on these features. This matters because when a programmer learns how a function works in Javascript, he learns how everything works.
Whereas when adding the same feature to Java, it's just being bolted on the side. So when a programmer learns how these new so-called closures work, all he learns is about a feature that isn't being used as a component of anything else, it's just a feature. We add complexity but get very little in return, much less than from the same feature in a language like Javascript.
I am not trying to get into a flame-fest over Java, but I do think that there comes a time when any product matures, and "improvements" should be viewed with extreme caution because the product has reached a point of greatly diminishing returns. I suggest that Java is in that place.
Essentially, this will replace the horrible callback syntax we have and have been using for a while. The new features will be integrated slowly, but they will be used.