Reader small image

You're reading from  Implementing Domain-Specific Languages with Xtext and Xtend

Product typeBook
Published inAug 2013
PublisherPackt
ISBN-139781782160304
Edition1st Edition
Right arrow
Author (1)
Lorenzo Bettini
Lorenzo Bettini
author image
Lorenzo Bettini

Lorenzo Bettini is an associate professor in computer science at the Dipartimento di Statistica, Informatica, Applicazioni "Giuseppe Parenti," Universit di Firenze, Italy. Previously, he was a researcher in computer science at Dipartimento di Informatica, Universit di Torino, Italy. He also was a Postdoc and a contractual researcher at Dipartimento di Sistemi e Informatica, Universit di Firenze, Italy. He has a masters degree summa cum laude in computer science and a PhD in "Logics and Theoretical Computer Science." His research interests cover design, theory, and the implementation of programming languages (in particular, objectoriented languages and network-aware languages). He has been using Xtext since version 0.7. He has used Xtext and Xtend for implementing many domain-specific languages and Java-like programming languages. He also contributed to Xtext, and he recently became an Xtext committer. He is the author of the first edition of the book "Implementing Domain-Specific Languages with Xtext and Xtend", published by Packt Publishing (August 21, 2013). He is also the author of about 80 papers published in international conferences and international journals. You can contact him at http://www.lorenzobettini.it.
Read more about Lorenzo Bettini

Right arrow

Chapter 3. The Xtend Programming Language

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.

An introduction to 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 – a better Java with less "noise"


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...

Debugging Xtend code


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...

Summary


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.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Implementing Domain-Specific Languages with Xtext and Xtend
Published in: Aug 2013Publisher: PacktISBN-13: 9781782160304
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Lorenzo Bettini

Lorenzo Bettini is an associate professor in computer science at the Dipartimento di Statistica, Informatica, Applicazioni "Giuseppe Parenti," Universit di Firenze, Italy. Previously, he was a researcher in computer science at Dipartimento di Informatica, Universit di Torino, Italy. He also was a Postdoc and a contractual researcher at Dipartimento di Sistemi e Informatica, Universit di Firenze, Italy. He has a masters degree summa cum laude in computer science and a PhD in "Logics and Theoretical Computer Science." His research interests cover design, theory, and the implementation of programming languages (in particular, objectoriented languages and network-aware languages). He has been using Xtext since version 0.7. He has used Xtext and Xtend for implementing many domain-specific languages and Java-like programming languages. He also contributed to Xtext, and he recently became an Xtext committer. He is the author of the first edition of the book "Implementing Domain-Specific Languages with Xtext and Xtend", published by Packt Publishing (August 21, 2013). He is also the author of about 80 papers published in international conferences and international journals. You can contact him at http://www.lorenzobettini.it.
Read more about Lorenzo Bettini