Xtend for Java Developers

There are a couple of common pitfalls when a Java developer starts using Xtend.

Java Xtend Description
String.class typeof(String) Get the class instance of a type
Long.MAX_VALUE Long::MAX_VALUE Accessing static fields
Foo.Bar Foo$Bar Accessing inner classes

Example: org.slf4j logging

private Logger log = LoggerFactory.getLogger(Foo.class)    // Java

        Logger log = LoggerFactory::getLogger(typeof(Foo)) // Xtend

Also, the .. or upTo operator has a severe bug. The code for i: 0..list.size won’t work as expected. First of all, it will iterate once too many.

The obvious fix for i: 0..(list.size-1) doesn’t work when the list is empty because it will iterate twice (0, -1) and it won’t iterate at all if the list has a single element.

Use .. only with constant operands (i.e. 1..5 is OK, list.size..0 isn’t). If you need to iterate over a range [start … end), use this gist instead.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.