I have been playing with two languages recently: Scala and Xtend.
Xtend and Scala have some similarities, but don’t let this make you think they are that similar. Both are JVM based languages offering a refresh over Java, but the itch they focus on is different and the culture behind them is even more different.
Xtend focuses on fixing Java so that it is good enough to do the stuff you are already doing with Java without so much pain. It is like bringing Java to the state where C# is, which is something Sun and Oracle haven’t been able to do. Xtend compiles to Java source code, not bytecode. If you use it on eclipse, you will see a xtend-gen folder with the generated code, which is then in turn compiled to bytecode. Everything is transparent to the developer. Xtend is built on top of Xtext, a framework to write Domain Specific Languages and get Java/IDE support for free (example: a Cucumber-like DSL)
Scala is also a “better Java”, but it focuses more on providing a “Scalable Language”. A language that can be used on different paradigms or as Domain Specific Languages (eg. the Play! web framework features type-safe HTML templates thanks to Scala). It mixes heavily the object oriented paradigm with functional programming. The functional aspect of Scala aims to provide support to designing concurrent programs easily. This goes beyond simple lambdas and the philosophy aims of immutable objects and expressions over statements, immutable collections and actors in the library.
For developers just looking for a refresh, both provide:
Optional Semicolons
Yes. No need to write them in most cases.
val and var
In Scala:
val someVariable : String = "This will not change" var someVariable : String = "This can change later"
In Xtend:
val String someVariable = "This will not change" var String someVariable = "This can change later"
While “val” is not different from “final”, I really like this syntax. In scala it is heavily used to make your brain always think if you really need
to change a variable later. You soon realize that you don’t, and most variables are calculations that need to be initialized once.
Type inference
In the example above, you don’t need to specify the types. They will be infered:
val someVariable = "This will not change" var someVariable = "This can change later"
You want to specify it for some field members that will not be initialized. Or if you assign a concrete class (e.g. HashMap) but wan’t the variable to be a Map.
Scala also has a nice feature, called “lazy val”:
lazy val someVariable = new VerySlowToConstructClass()
In this case, the variable will be initialized the first time it is accessed.
Lambdas
In Xtend
val lambda = [String s | s.length]
In Scala
val lambda = (s : String) => { s.length }
Both languages have shorter versions for special cases (no parameters, one parameter, etc). I am not going to describe them here.
Something really cool in Xtend: if you have an interface with one method used as a callback, the compiler will convert a lambda automatically to the interface type.
final JTextField textField = new JTextField();
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.setText("Something happened!");
}
});
Can be described using a lambda in Xtend like:
val textField = new JTextField textField.addActionListener([ ActionEvent e | textField.text = "Something happened!" ])
Or, using the shorter lambda version I did not describe:
val textField = new JTextField textField.addActionListener [ textField.text = "Something happened!" ]
Lambdas are closures, so they take variables from the current scope. Lambdas are not only useful for callbacks. I do miss ruby’s each/map/collect in Java:
movies.filter[ (1980..1989).contains(year) ].sortBy[ rating ].last.year)
Or map/reduce
val charCount = strings.map[s|s.length].reduce[sum, size | sum + size]
Extending libraries
Xtend has a really cool feature called extensions.
"hello".toFirstUpper // calls StringExtensions.toFirstUpper(String) listOfStrings.map[ toUpperCase ] // calls ListExtensions.<T, R>map(List<T> list, Function<? super T, ? extends R> mapFunction)
Xtend already includes quite a lot of extensions for Java classes like String. Some are convenience methods, but some support features, like the map example above. It is not very useful to support lambdas if your collections do not have forEach or map. I like how this feature is implemented. Simple and elegant. There is more awesomeness in this area. You can find about it in the guide.
Scala on the other hand has something called implicit convertions. They can be used with a pattern called “pimp-my-library” to extend existing APIs:
For example, to add a method headOr to the List class, one first create a “wrapper” class with the method:
class ListExtensions[A](xs : List[A]) {
def headOr(f : => A) : A = xs match {
case h :: _ => h
case Nil => f
}
}
Then you add a implicit conversion to this wrapper class:
implicit def listExtensions[A](xs : List[A]) = new ListExtensions(xs)
And then this should work:
println(List(1,2,3).headOr(0)) ==> 1
Implicit conversions in Scala can be used to use lambdas as event listeners:
implicit def function2ViewOnClickListener(f: View => Unit) : View.OnClickListener = {
new View.OnClickListener() {
def onClick(view: View) {
f(view)
}
}
}
Then this works:
loginButton.setOnClickListener((v: View) => { println("CLICK!") } ))
This is not automatic as in Xtend. When I was playing with Scala & Android what I did was to create a “trait ScalaActivity extends Activity” with the implicit conversion and then make my activity “class MainActivity extends Activity with ScalaActivity”. Traits are cool, give them a look.
There is more stuff in both languages I am not going to spend time on, but you can go to the respective documentation.
For Xtend go to the documentation or this document called “20 Facts about Xtend”.
For Scala. I bought “the book”. However you can also find more in the documentation site.
IDE support
I first tried Scala a year ago (can’t remember) and the IDE support was so bad that I did not go further. This is no longer true. There has been quite a lot of investment in it lately and it is good enough already.
Xtend is part of Eclipse now. Therefore you can expect good IDE support. I found some glitches and weird messages, but in general it works fine.
Android
I tried both languages on Android.
Scala worked fine, but you have to use ProGuard to reduce the size of the application by removing unused methods and classes.
This guide was a good start. However I hit weird errors with the Treeshaker Proguard plugin I was using. This Stackoverflow answer put me back on the right track with the right ProGuard plugin.
I don’t feel confortable with Scala on Android. Without an external tool to trim the jar the generated code goes over the limit of methods that can be handled, even when developing. Not only that. The scala runtime library is 8.7M:
-rw-r--r-- 1 duncan users 8.7M Sep 17 00:01 org.scala-ide.scala.library_2.10.0.v20120820-123254-M7-1ab4994990.jar
Xtend was a surprise. Theoretically, as it just generates Java code, it should kind of work out of the box. This was not the case. It was harder than with Scala.
I spend quite some time trying to use the “R” Android class with Xtend. Basically:
setContentView(R.layout.activity_main);
And the answer was not intuitive for me.
setContentView(R$layout::activity_main)
This is a syntax issue. It is documented. But it was just too unexpected for me.
There is another issue you should know about. No debugging. The Dalvik VM does not support JSR-45 which is why debugging Xtend (and other Xbase languages) doesn’t work.
Once I accepted not being able to debug. I ran my program:
09-29 21:26:02.794: I/dalvikvm(11174): Failed resolving Lduncan/test/MainActivity$5; interface 514 'Lorg/eclipse/xtext/xbase/lib/Functions$Function1;'
Ok. The Xtend runtime libraries need to be put into the apk. I went to the build settings “Export and Order” and marked the Xtend library.
[2012-09-29 23:40:08 - Myapp] Error generating final archive: Found duplicate file for APK: about.html Origin 1: /space/sw/eclipse/plugins/org.eclipse.xtend.lib_2.4.0.v201208210511.jar Origin 2: /space/sw/eclipse/plugins/com.google.guava_10.0.1.v201203051515.jar
Ok, if I delete these files, everything will work (of course it will break again when I update Xtend with Eclipse:
zip -d /space/sw/eclipse/plugins/org.eclipse.xtend.lib_2.4.0.v201208210511.jar about.html deleting: about.html zip -d /space/sw/eclipse/plugins/org.eclipse.xtext.xbase.lib_2.4.0.v201208210511.jar about.html
Run again:
[2012-09-29 23:44:07 - MyApp] Error generating final archive: Found duplicate file for APK: plugin.properties Origin 1: /space/sw/eclipse/plugins/org.eclipse.xtend.lib_2.4.0.v201208210511.jar Origin 2: /space/sw/eclipse/plugins/org.eclipse.xtext.xbase.lib_2.4.0.v201208210511.jar
Ok. Now I get it. These are Eclipse plugins. There will be always duplicated files. This is not the solution.
What I did was to unmark the “Export and Order” check I did to “Xtend Libraries”. Copy xtend.lib, xbase.lib and guava jars from the plugins into libs/ of my project. And remove about.html and plugin.properties from these 3 jars. Basically I did a un-OSGi version of the jars.
Also, a good tip is to change the “xtend-gen” folder in the Xtend settings to use the “gen” folder Android already uses to dump generated files from resources and others.
After that, everything worked fine and I got my application running on Android. The size of the Xtend and Xbase libraries is 7K and 90K. guava is the heaviest dependency with 1.2M. But nothing compared to Scala. My application was 2M installed without proguard processing (which happens by default in release mode).
Criticism
Scala
In my opinion, Scala is much more mature. The syntax is well thought. I was annoyed for using [] for generics and () to index arrays until I saw the explanation in the book. I am enjoying reading the book and learning the “Scala way”.
Scala has a unified type system. Unlike Java, everything is an object. You can write:
"some text".length 1.abs
The problem starts when you realize there is Null, null, Nil, Nothing, None, and Unit (a.k.a void). Also Any, AnyRef and AnyVal. If you want to learn about those you can read this post. Or you look at scala.collections.immutable.List to look for more information about the type:
sealed abstract class List[+A] extends LinearSeq[A] with Product with GenericTraversableTemplate[A, List] with LinearSeqOptimized[A, List[A]]
Oh, and there is a generic called “Either[+A, +B]“.
I know there is a reason for this. I know at some point a page in the Scala book will explain it. But the step curve is not easy. I was writing very basic code and I was asking myself how to do lot of stuff. The biggest problem in my opinion is that Scala, being “Scalable”, allows to write the code in various ways. One is the Scala way. The other is not. Sometimes one is the intuitive one. Sometimes the other is to slow.
An example of this is the Quick Pimp Library Pattern. This allows you to write the explicit conversion easier by not having to create a “wrapper” type.
implicit def string2toInt(s: String): { def toInt: Int } = new {
def toInt: Int = java.lang.Integer.parseInt(s)
}
But for some reason (see the link) this is slow. There was also some speed issue with “for” loops if you write the “for” in the wrong way (I forgot the details).
This gives me a feeling of the Scala culture very similar to what some subworld of C++ doing heavy template meta-programming, policy based design gives to me. I like boost. But I have to be very awake to decode the signatures and understand how to use a library. The design and the quality is top-notch, but it is an advanced device. You need to invest quite a bunch of time on it and make sure those features will actually pay off. I am not against a language that requires some CS background and a type system that requires you to think a bit. But if you are going to master it, you have to be aware of the cost/benefits.
Xtend
Xtend was a pleasure to write and read. I got used to it very fast. The documentation is a single page guide. The type system is the same as Java.
However, Xtend is not fully mature yet (despite version being 2.x). I was playing with the JFugue library and I tried to do this:
rhythm.addSubstitution('O', "[BASS_DRUM]i")
But I got an error, because the char was interpreted as an string. Then I realized that Xtend does not have character literals yet. The solution:
rhythm.addSubstitution('O'.charAt(0), "[BASS_DRUM]i")
Then when playing with Android, I tried to use one of the greatest features: automatic conversion of lambdas to interfaces with one callback method:
button.onClickListener = [ this.textView.text = "Foo" ]
But it did not compile! The simplest example did not work. Disclaimer: I was not using the released version. But I was not using the nightly builds either. I was using the milestones. I got a “Incompatible types” error. I updated the eclipse plugin to the current milestone, restarted Eclipse, and everything was working. (Facepalm).
Conclusions
Both are great languages. If you are doing servers and services, I’d pick Scala. But be prepared to invest some time learning the culture behind it.
Learning Scala gives you the most from the Play! Framework. It can also be used from Java, but it is not the same.
If you are only looking for a Java refresh. Go with Xtend. You will learn it in one night and will benefit from the value it provides. Once the quirks with Android get resolved (especially the debugger), it is a great addition to Android development. Being an Eclipse project means the IDE will be a first citizen and releases will be stable enough to ship with Eclipse itself.






