Xtend
| Company type | Open source programming language |
|---|---|
| Industry | Software development |
| Founded | June 2011 |
| Headquarters | Eclipse.org community |
| Products | Programming language for the Java Virtual Machine |
| Owner | Eclipse.org community |
| Website | www |
Xtend is a versatile, high-level programming language designed for the Java Virtual Machine. While it takes both syntax and semantics cues from Java, it focuses on a more streamlined syntax and introduces enhancements such as type inference, extension methods, and operator overloading. Although primarily an object-oriented language, Xtend integrates functional programming concepts like lambda expressions. It employs static typing by utilizing Java's existing type system unchanged and compiles down to Java source code, ensuring smooth interoperability with Java libraries.
Created by the Eclipse.org community, Xtend is included in the yearly Eclipse release train and is distributed as open source under the Eclipse Public License. It can be compiled and run independently from the Eclipse IDE.
History
Xtend was developed as an offshoot of Xtext, the framework responsible for defining both its language structure and editor support. The language was first introduced in June 2011 during the Eclipse Indigo release. From Eclipse Juno (June 2012) onward, starting with Xtend version 2.3, it has been maintained as a separate project.
This version of Xtend should not be mistaken for an earlier language sharing the same name from the Xpand project. Originally labeled Xtend2 to differentiate it, the numeric suffix was later removed. With its support for template expressions, Xtend seeks to fully supersede the Xpand technology.
Philosophy
Java remains one of the most widely used programming languages with an extensive ecosystem. However, its syntax is often criticized for verbosity and slow adoption of new features. Xtend’s goal is to preserve Java’s advantages while simplifying syntax and adding capabilities that promote clearer and more succinct code.
The language’s syntax is intentionally similar to Java’s to facilitate easy adoption by Java developers. By compiling into Java code and relying on Java’s type system, Xtend allows seamless integration of both languages within a single project.
Extensibility is a core design principle, supported through lambda expressions and extension methods. This approach permits feature expansion via libraries without modifying the underlying language. Its minimal standard library takes advantage of this extensibility model.
The Eclipse-based IDE for Xtend offers comprehensive support including syntax highlighting, code completion, refactoring tools, navigation aids, and debugging features, all integrated with Eclipse’s Java Development Toolkit.
Semantics
Xtend exhibits numerous parallels with Java. For instance:
<syntaxhighlight lang="xtend"> package sample
import java.util.List
class Greeter {
def greetThem(List<String> names) {
for(name: names) {
println(name.sayHello)
}
}
def sayHello(String name) {
'Hello ' + name + '!'
}
} </syntaxhighlight>
The language supports type inference for variables as well as method return types. By default, classes and methods are public while fields are private. Use of semicolons is optional.
The sayHello method functions as an extension method, called as if it were a member of its first parameter. Extension methods can originate from other classes or instances.
A functional programming style alternative leverages lambda expressions combined with higher-order functions:
<syntaxhighlight lang="xtend"> def greetThem(List<String> names) {
names.forEach [ println(sayHello) ]
} </syntaxhighlight>
In this context, the lambda parameter implicitly uses it with an inferred type. Lambdas are automatically converted to single-method interfaces, allowing them to be used as implementations of interfaces such as java.lang.Comparable.
Template expressions provide multi-line strings supporting interpolation within triple quotes:
<syntaxhighlight lang="xtend"> def sayHello(String name)
Hello «name» !
</syntaxhighlight>
Xtend intelligently handles whitespace to ensure that generated source code adheres to formatting standards.
Other notable features include multimethods, advanced switch expressions, and operator overloading implemented through library methods.
Bibliography
- Lorenzo Bettini, Implementing Domain-Specific Languages with Xtext and Xtend - Second Edition, Packt Publishing, 2016
External links
- Sven Efftinge, Sebastian Zarnekow, Enhancing Java, Pragmatic Programmer Magazine, December 2011.
- Alex Blewitt: Xtend Enhances Java, Interview featuring Sven Efftinge, InfoQ, June 2012.