Thursday, October 20, 2011

Xtend 2.1 - Type-safe Groovy-style Builder API

The upcoming release of Xtend (Version 2.1, November 2nd) is going to be a huge leap forward. Besides the many new editor features, we managed to improve the performance of linking and type inference by many times. The editing experience is now very close to what you are used to from Eclipse's Java tooling. We are working on a comprehensive New & Noteworthy page where all the cool new features will be listed. Everything will be ready for EclipseCon Europe on November 2nd.

In this post I want to focus on the new language features we added. There are mainly two of them, which are both interesting for themselves but even more so in combination.


The Implicit Variable 'it'

If you name your variable 'it' all its members are accessible without qualifying them with the variable reference (just like 'this').

Example:


def appendFoo(StringBuilder it) {
  append('foo')
}

Also note that variables with the name 'it' are the only ones which can be overridden in lexical scopes. So you could for instance write the following, where the closure shadows the outer variable it.


def foo(List<String> it) {
  foreach( it | toUpperCase )
}

You should understand the other new language features to see how this is meant to be used.



Sugar For Closures With One Argument

Many languages have special syntactic sugar for closures with just one argument. So does Xtend 2.1. The general syntax for a closure with one argument is :


[Type arg | expression-using-arg ]

With zero arguments it still is


[| expression-using-only-outer-context ]

And the new sugared version for closures with one argument is:


[expression-using-it]

And of course the name of the implicit argument is 'it'.



Closures As Last Arguments

We added another feature, which is common in many other popular languages: If you invoke a method where the last argument is a closure you can place the closure after the
actual method invocation.

Example:


newArrayList('a','b','c').foreach [ toUpperCase ]


Groovy-style Builder Syntax

Using these three language enhancements in combination allows for defining statically-typed groovy-style builder APIs. Check the screencast to see it in action.



If you are attending EclipseCon Europe and would like to know more about the possibilities of Xtend, make sure to go to Sebastian's session.

3 Kommentare:

Hallvard Trætteberg said...

I like the use of it for simplifying certain expressions. The closure-after-method-call notation would be even nicer if a closure looked more like an ordinary block!

On the forum, you mentioned a new syntax for creating object trees with a combined let/with construct. Will that be part of this release?

Sven Efftinge said...

Hi Hallvard,

what is described in the blog post has replaced the design I explained in the forum.

saml said...

Hi Sven, What would you describe as the main difference between Groovy and Xtend? Or, more specifically, why (or when) should I use Xtend rather than Groovy (being mainly a normal Java developer so far)?