Suvarna Garge (Editor)

Xtend

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Developer
  
itemis

First appeared
  
2011

Paradigm
  
Object-oriented, imperative, functional

Designed by
  
Sven Efftinge, Sebastian Zarnekow

Stable release
  
2.11.0 / February 1, 2017; 53 days ago (2017-02-01)

Typing discipline
  
Static, strong, inferred

Xtend is a general-purpose high-level programming language for the Java Virtual Machine. Syntactically and semantically Xtend has its roots in the Java programming language but focuses on a more concise syntax and some additional functionality such as type inference, extension methods, and operator overloading. Being primarily an object-oriented language, it also integrates features known from functional programming, e.g. lambda expressions. Xtend is statically typed and uses Java's type system without modifications. It is compiled to Java code and thereby seamlessly integrates with all existing Java libraries.

Contents

The language Xtend and its IDE is developed as a project at Eclipse.org and participates in the annual Eclipse release train. The code is open source under the Eclipse Public License. Yet, the language can be compiled and run independent of the Eclipse platform.

History

Xtend originated from Xtext, which is the technology used to define the language and the editor. Xtend was first released as part of Xtext in the Eclipse release Indigo in June 2011. Since the Eclipse release Juno (June 2012, Xtend version 2.3) Xtend has become a standalone project.

The language Xtend described here should not be confused with the older language with the same name in the Xpand project. Initially, Xtend was named Xtend2 for better distinction. The '2' was dropped soon for simplicity. With its template expressions, Xtend is meant as a replacement of the entire Xpand technology.

Philosophy

Java is one of the most popular programming languages ever, and it has grown an enormous ecosystem of libraries and tools throughout the years. Yet, its syntax is quite verbose, and some concepts are missing and only added very slowly. Xtend tries to get the best of Java, but kill the syntactic noise and add essential new features to allow for better readable and more powerful code.

To make it easier to learn for Java developers, Xtend's syntax is close to Java's. Xtend maintains maximum compatibility with Java by compiling to Java code and using Java's type system. Java code and Xtend code can be mixed inside the same project at will.

Using a combination of lambda expressions and extension methods, the language can be extended by means of libraries, i.e. without changing the language itself. A small standard library makes heavy use of this.

Xtend has always been designed with good tooling in mind. The Eclipse-based Xtend IDE offers things like syntax highlighting, code completion, refactoring, navigation and debugging. It also integrates tightly with Eclipse's Java Development Toolkit.

Semantics

Xtend resembles Java in many regards. Here is an example Xtend file:

Xtend provides type inference, i.e. the type of name and the return types of the methods can be inferred from the context. Classes and methods are public by default, fields private. Semicolons are optional.

The example also shows the method sayHello called as an extension method, i.e. like a feature of its first argument. Extension methods can also be provided by other classes or instances.

Instead of using the imperative for-loop, one could use a functional style lambda expression in square brackets and call the higher-order function forEach in extension syntax on the list:

Note that the lambda's parameter, if not specified, is called it, which can be skipped like this in Java. Its type is inferred as string. Lambda expressions are also automatically coerced to single method interfaces, such that they can be passed e.g. as a java.lang.Comparable.

Template expressions are multi-line strings within triple quotes with interpolated values in French quotes. In the example above one could write

Xtend offers intelligent white-space management - the above text will not be indented in the output - thus meeting the requirements of code generation.

Further language features include multimethods, a powerful switch expression, and operator overloading by means of library methods.

References

Xtend Wikipedia