Introduction

Xtend is a statically-typed programming language which is tightly integrated with and runs on the Java Virtual Machine. It has its roots in the Java programming language but improves on many concepts:

It is not aiming at replacing Java all together. Therefore its library is a thin layer on top of the Java Development Kit (JDK) and interacts with Java exactly the same as it interacts with Xtend code. Java can call Xtend methods, too, in a completely transparent way. And of course, Xtend provides a modern Eclipse-based IDE closely integrated with Eclipse's Java Development Tools (JDT).

Getting Started

Xtend requires Eclipse 3.5 or higher and a Java SDK version 5 or above. The easiest way to install the SDK is via Eclipse Marketplace. There even is a client integrated into Eclipse. You'll find it in the Help-menu.

Hello World

Let us start with a simple "Hello World" example. In Xtend, that reads as

class HelloWorld {
  def static void main(String[] args) {
    println("Hello World")
  }
}

The Xtend code resembles Java a lot. You can already see how the syntactic noise is reduced: No semicolons, no return types etc.

An Xtend class resides in a plain Java project. As soon as the SDK is installed, Eclipse will automatically translate it to Java code. You'll find it in a source folder xtend-gen. The hello world example is translated to the following Java code:

import org.eclipse.xtext.xbase.lib.InputOutput;

public class HelloWorld {
  public static void main(final String[] args) {
    InputOutput.<String>println("Hello World");
  }
}

The Runtime Library

The only surprising fact in the generated Java code may be the library class InputOutput. Many features of Xtend are not built into the language itself but provided via the library org.eclipse.xtend2.lib. The library is available from a Maven repository (check the website for details) and provides a lot of useful functionality annotating existing types from Java's SDK.

It provides means to create collections in a readable way:

val myList = newArrayList(1, 2, 3)
val mySet = newHashSet(4, 5, 6)
val myMap = newHashMap(1 -> 'one', 2 -> 'two', 3 -> 'three')

It also extends the collection types with a lot of very useful functions. One example is the ubiquitous map function:

val listOfNames = myList.map( e | myMap.get(e) )

Operators to concat collections or to do arithmetics with types like BigDecimal are also available.

The Xtend Tutorial

The best way to get acquainted with the language is to materialize the Xtend Tutorial example project in your workspace. You will find it in the New > Project.. wizard dialog.

The project contains a couple of sample Xtend files which show the different language concepts in action. Looking into the xtend-gen folder which holds the compiled Java code will help you understand the concepts better.

Maven Support

The runtime library as well as a plugin to run the compiler in a Maven build can be be obtained from the following maven repository: http://build.eclipse.org/common/xtend/maven/. We are working on having these artifacts in Maven Central soon.

Here's the XML for the library dependency:

<dependency>
  <groupId>org.eclipse.xtend2</groupId>
  <artifactId>org.eclipse.xtend2.lib</artifactId>
  <version>2.2.0</version>
</dependency>

And this is the XML for the plugin:

<plugin>
  <groupId>org.eclipse.xtend2</groupId>
  <artifactId>xtend-maven-plugin</artifactId>
  <version>2.2.0</version>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
        <goal>testCompile</goal>
      </goals>
    </execution>
  </executions>
</plugin>