Freitag, 18. Juli 2014

RxJava and Xtend

Today I wanted to contribute an Xtend language extension for RxJava. Other languages like Scala, Groovy or Kotlin had already done so, so it felt natural that there should also be one for Xtend.

But looking through the examples and trying out the APIs I quickly found that no special library is needed. Xtend has all the necessary convenience. The most important feature is SAM type conversion just like in Java 8. So anywhere RxJava expects a functional interface, you can just use an Xtend lambda.

This already eliminates most of the interop headaches that other languages like Scala have. They needed to rewrite a huge part of the API from scratch (several thousand lines of code!) in order to make their lambda syntax work.

Xtend will even do the right thing in pretty ambiguous situations like Observable.create(). This is overloaded with two functional interfaces. One takes a Subscriber and returns nothing. The other takes an Observer and returns a Subscription (and is deprecated).

Last but not least, Xtend makes RxJava even more readable by making the Observable.lift() method unnecessary. The lift() method is normally used to fluently call your own Operators. But with Xtend you can instead define an extension method and then call it directly on the Observable as if it was an instance method.