Tuesday, October 25, 2011

This Is Not Your Father's Java

The Java language and so its community has traditionally been relatively conservative when it comes to API design and language features. After all Java seems to be the very last language on earth without closures! Also a lot of other language features are highly demanded by many people.

With limits come creativity! 


To compensate for the verbose language and missing convenience, the Java community created the three single best IDEs in the world. Also the popularity and maturity of code generation and language add-ons like AspectJ or even XML stems from the limitations in Java as a language. With the rise of alternative languages for the JVM, Java folks started to think out of the box. Modern API design is pushing Java to its limits and with some creativity you can even emulate missing language features.

Next week at EclipseCon Europe I'll be giving a talk on creative API design in Java.

The talk will be structured like a quiz: I show a working piece of Java code to the audience and explain what it does. Then you tell me how it does so. So it's a slightly similar structure to the popular Java Puzzlers talks but in this case the puzzlers are actually unconventional but useful idioms. I expect a lively discussion about the pros and cons of the various idioms and I'm almost sure there is something new for everybody. Feel challenged? Come to the session! :-)

Thursday, October 20, 2011

Xtend - Type-safe Groovy-style Builder API

In this post I want to focus on two cool language features 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 lambda expressions argument shadows the outer variable it.

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

Sugar For Lambdas With One Argument

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

  [Type arg | expression-using-arg ]

But as lambdas with one argument are a common case, you can leave out the paramter declaration as in :

  [expression-using-it]

And of course the name of the single argument will be '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.

Interesting? See xtend-lang.org for more cool features.