In this chapter, we will introduce the Xtend programming language, a fully-featured Java-like language that is tightly integrated with Java. Xtend has a more concise syntax than Java and provides additional features such as type inference, extension methods, and lambda expressions, not to mention multi-line template expressions (which are useful when writing code generators). All the aspects of a DSL implemented in Xtext can be implemented in Xtend instead of Java, since it is easier to use and allows you to write better-readable code. Since Xtend is completely interoperable with Java, you can reuse all the Java libraries; moreover, all the Eclipse JDT (Java Development Tools) will work with Xtend seamlessly.
You're reading from Implementing Domain-Specific Languages with Xtext and Xtend
The Xtend programming language comes with very nice documentation, which can be found on its website, http://www.eclipse.org/xtend. We will give an overview of Xtend in this chapter, but we strongly suggest that you then go through the Xtend documentation thoroughly. Xtend itself is implemented in Xtext and it is a proof of concept of how involved a language implemented in Xtext can be.
We will use Xtend throughout this book to write all parts of a DSL implementation. Namely, we will use it to customize UI features, to write tests, to implement constraint checks, and to write code generators or interpreters for all the example DSLs we will develop in this book. In particular, starting with version 2.4, all the stub classes generated by Xtext for your DSL projects are Xtend classes by default (instead of Java, as in the previous versions).
You can still generate Java stub classes by customizing the MWE2 workflow, but in this book we will always use Xtend classes. Xtend...
Xtend is a statically typed language and it uses the Java type system (including Java generics). Thus Xtend and Java are completely interoperable.
Most of the linguistic concepts of Xtend are very similar to Java, that is, classes, interfaces, and methods. Moreover, Xtend supports most of the features of Java annotations. One of the goals of Xtend is to have a less "noisy" version of Java; indeed, in Java, some linguistic features are redundant and only make programs more verbose.
Let's write a "Hello World" program in Xtend:
package org.example.xtend.examples class XtendHelloWorld { def static void main(String[] args) { println("Hello World") } }
You can see that it is similar to Java, though there are some differences. First of all, the missing semicolons ;
are not mistakes; in Xtend, they are not required (though they can be used). All method declarations start with either def
or override
(explained later in the chapter). Methods are public...
The Java code generated by Xtend is clean and easy to debug. However, it is also possible to debug Xtend code directly (instead of the generated Java), thanks to the complete integration of Xtend with the Eclipse JDT debugger.
This means that you can start debugging a Java application that at some point invokes Java code that has been generated by Xtend and, stepping through that, automatically brings you to debugging the original Xtend source. The next screenshot shows a debugging session of Xtend code. Note that all the JDT debugger views are available; furthermore, implicit variables such as it
can be inspected in the Variables view:
You can also debug an Xtend file (for example, containing a main
method) directly, since all the Run and Debug configuration launches are available for Xtend files as well. Breakpoints can be inserted in an Xtend file by double-clicking on the breakpoint ruler in the editor (currently, the Debug context menu is not available for Xtend files...
Xtend provides many features that allow you to write clean and more readable code. Since it is completely integrated with Java, all the Java libraries are accessible from within Xtend; moreover, the IDE tooling of Xtend itself is basically the same as the ones of JDT. For all of the aforementioned reasons, Xtext fosters the use of Xtend to develop all the aspects of a DSL implementation.
In the next chapter, we will show you how to implement constraint checks for a DSL using the EMF Validator mechanism using the Xtext enhanced API.