Xtend is a new language intended for Java developers that compiles to Java code. Is this a good idea? Where does it fit in to the existing landscape of programming languages?
Just when you thought that no-one in their right mind would launch another high-profile language to add to Go, Dart, Kotlin, Ceylon, not to mention more established new languages such as CoffeeScript, Clojure, Scala and so on, the Eclipse foundation aims to surprise. Xtend is a very odd idea and not quite what you might expect at all. It is a Java-like language but, according to the publicity, not intended to replace Java but to improve it. It is designed to be,
A language made for Java developers
but what does this mean?
At first look it seems to be Java with some extra bits. It compiles to Java code. Yes you heard that correctly - it produces human readable Java not machine executable byte code. This is supposed to mean that you can use your Java knowledge to go in and edit the compiled source code. You don't have to think long and hard to see that this isn't really a good idea. Machine generated code is rarely easy to read and modifying it isn't a fun way to pass the time. This said Xtend comes with good Eclipse support.
The interesting news is that it has been implemented using the Xtext system, a tool to enable the easy creation of Domain Specific Languages. In this light Xtend is a really impressive demonstration of how powerful Xtext is. As an advert for Xtext it wins instantly.
Details of the language indicate that it starts from Java, with similar syntax and an identical static type system, and it uses the JDK as its library - given it compiles to Java any other choices would have been difficult. One big difference is that it only does classes. That's correct no interfaces, no enums and no annotation types.
Additions to Java-like behavior include automatic type inference, which means even though the language is statically typed you mostly don't have to specify the type at all. For example, both:
val String word="Hello"
Another welcome upgrade to Java is the automation of getters and setters. For example, if you write:
then this is the same as
is the same as
There are a few other syntactic variations but what about something more semantic, something that really extends Java? Xtend has a collection of trendy new features, some of which will be added to Java itself in the next version. The big two are closures and extension methods. A closure in this case also means an anonymous function as well as the function capturing the current scope. Extension methods are used to add methods to built-in or otherwise closed classes and they can be useful.
Other improvements include a better and less error-prone switch expression; method dispatch based on runtime rather than compile time; type; and some fancy facilities for building complicated formatted strings with template expressions.
No doubt there are other features of Xtend that I have failed to mention but unless there is something really big lurking, and I doubt it, the conclusion has to be the same. Using a translator to convert one language to a well-known and well-supported language has its advantages. It also has disadvantages and, while the tools for compiling Xtend to Java look good, you stil have to work in two languages if you are to take advantage of the translation.
Is Xtend better than Java? The answer has to be "yes" in that it cleans up a lot of the "noise" still present in Java and it adds some nice features - closures in particular. However, Java is evolving and if you wait a little while Java will have closures and other shiny new features.
The bottom line, then, is the simple fact that it probably isn't worth risking your project to a language that has no track record and might well vanish next week.
So overall, it's a nice try but no thanks.
As for Xtext as a way of building DSLs - that really does look as it if deserves some investigation.
To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter or Facebook or sign up for our weekly newsletter