February 4, 2014

Xtend

It has been a while, but it is time for another post.

After my love/hate relationship with Scala went to a sour end I continued to work with Java and for the most part have quiet enjoyed working with tools and libraries which not only work as expected but pretty much always work as intended. Recently, I have been working on a compiler for a subset of Java as part of a project. It has been very interesting to see how its all done and even learning about compiler compilers and how to do the various types of parsing (LL(1), LR(0), SLR(1), LALR(1), etc...). All of a sudden in the middle of all this I had a brilliant idea, why don't I make a simple little compiler that takes some of the simple key features from Scala I like and make a language extension to Java!

No need for the more exotic features of Scala, a brand new exotic runtime library, or byte code generation. I would simply emit Java source files and not have to write as much boilerplate code! Some basic type inference using var/val, make default visibility public (because who really has a real-world use case for package private), I could also create extension methods (because they are totally awesome), add properties to Java or operators/overloading, etc. Like all the features Java *should* have but doesn't. My thinking is along the lines of the fact things like Coffeescript and Typescript exist for Javascript. Java can compile down to Javascript for the most part (GWT). Additionally, if I make it compile down to Java 5/6 it will then work with Android without any compatibility issues.

However, I decided maybe it would be easier if this was done with some light-weight macro system which would run before the Java compiler. I'm not talking about C-style pre-processor here, I'm talking about a type-aware first-class citizen macroing system that we use in the 21st century. I decided it was time to do some quick searching because there is no way I am the first or only person to think about this (how to get rid of all the boilerplate from Java). So after a few searches and a bunch of people on Stackoverflow explaining why macros are bad something popped up called Xtend.

What is Xtend? Well, the name is pretty much what you think it means. It is an extension for Java that is not meant to be a full-blown language replacement but something which compiles down to Java source and is then compiled as Java code. It doesn't try to do any of the other fancy stuff you find in other JVM languages, because the goal is to make it 100% interoperable with Java. It has the exact same type system as Java and provides pretty much every feature I mentioned above, PLUS more. Additionally, it is designed to work well with Java 8's new API meaning I won't have to worry about future compatibility. It also creates Java 5 compatible files which means Android compatible out of box. It does have its own library, but unlike the other JVM languages it is a lightweight wrapper to simply provide syntactic sugar instead of new classes/structures/etc. Why is this good? Well, I get to continue to use everything I know without having to worry about cross-language library annoyances.

The best part so far is the version 2.5.1 has top-notch Eclipse integration, it works as described and no surprises. Mind you they really need to make it easier to create Xtend files by right-clicking on a project (maybe some Xtend perspective?!?!). The lambda structure feels a bit funky as I haven't seen anything like it before. Then again, Clojure also has a slightly odd style but it isn't too strange. Method dispatch and guards are very handy (think Scala's match construct). Everything has a return value, which is very Scala-like. It has value classes, just like Scala's case classes. It removes the default fall through behavior of switch statements (which is honestly the worst and most pointless legacy thing in any modern programming language). I could just list the features it offers but that would continue to make this post more massive. The really nice part is the compiler hooks, and allowing people to easily extend Xtend. I'm almost tempted to say some of the Java 6 concurrency API should be integrated more tightly as a future feature of Xtend, but for the time being I'm glad to have found a wonderful tool which provides the exact solution I've been search for! The other thing I might start looking at is Xtext; however, that appears to fall more into the DSL territory I refuse to dirty my hands in.

Seriously though, why haven't I found this sooner!?

No comments :

Post a Comment