Reader small image

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

Product typeBook
Published inAug 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781786464965
Edition2nd Edition
Languages
Tools
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. Working with the Xtend Programming Language

In this chapter, we will introduce the Xtend programming language, a fully featured general purpose Java-like language that is completely interoperable with Java. Xtend has a more concise syntax than Java and provides powerful features such as type inference, extension methods, dispatch methods, and lambda expressions, not to mention multiline 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.

This chapter will cover the following topics:

  • An introduction to the Xtend programming language

  • A description of the main features of Xtend, which we will use throughout the book

An introduction to Xtend


The Xtend programming language comes with very nice documentation, which can be found on its website, https://www.eclipse.org/xtend/documentation/. 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, all the stub classes generated by Xtext for your DSL projects are Xtend classes.

You can still generate Java stub classes by customizing the MWE2 workflow, but in this book, we will always use Xtend classes. Xtend, besides providing useful mechanisms for writing code generators...

Xtend – a better Java with less "noise"


Xtend is a statically typed language and it uses the Java type system, including Java generics and Java annotations. 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. 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.

The Xtend Eclipse editor supports the typical features of the Eclipse Java editor, including templates. Thus, we can create a main method inside the previously created Xtend class as shown in the following screenshot, using the content assist template proposal:

Let's write the "Hello World" print statement 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 the removal of syntactic noise...

Additional operators


Besides standard operators, Xtend has additional operators that help to keep the code compact.

Quite often, you will have to check whether an object is not null before invoking a method on it; otherwise, you may want to return null or simply perform no operation. As you will see in DSL development, this is quite a recurrent situation. Xtend provides the operator "?.", which is the null-safe version of the standard selection operator (the dot .). Writing o?.m corresponds to if (o != null) o.m. This is particularly useful when you have cascade selections, for example, o?.f?.m.

The Elvis operator ("?:") is another convenient operator for dealing with default values in case of null instances. It has the following semantics: x ?: y returns x if it is not null and y otherwise.

Combining the two operators allows you to set up default values easily, for example:

// equivalent to: if (o != null) o.toString else 'default'
result = o?.toString ?: 'default'

The with operator (or double...

Summary


Xtend provides many features that allow you to write clean and more readable code. Since it is completely interoperable 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 and 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. - Second Edition
Published in: Aug 2016Publisher: PacktISBN-13: 9781786464965
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 €14.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