Thursday, April 05, 2012

Xtend Examples used at EclipseCon 2012

As there were almost no slides in my EclipseCon session on Xtend, I decided to record the examples I used during the presentation. You can find the source code at github.

Here are the few slides I had:

Example 1 : Hello World

How to start given an empty workspace.

Example 2 : Hello SWT

SWT is well known at EclipseCon, so I decided to have another Hello World using SWT just to show how nice interacting with existing Java APIs (even older ones) can be with Xtend.
It mainly shows how to use lambda expressions where you would write clumsy anonymous classes in Java.

Example 3 : 99 Bottles Of Beer

An implementation of the 99 bottles song using Xtend. It shows the use of template expressions and extension methods.

Example 4 : Distances

The example I blogged about yesterday. Mainly covering operator overloading and extension methods.

Example 5 : HTML Builder

In this example first an HTML DOM is created before it is serialized and printed to the console. It shows how to create a builder API using extension methods and lambdas. It also demos a switch expression with type guards as an a alternative to clumsy instance-of cascades or visitors.


  1. Hi Sven,
    I am using Xtext and have an application which runs the Xtext builder to generate output files. I am able to mark only the files within the output directory as derived, however I want the output directory itself to be marked as derived.

    Is there a way to achieve this?

  2. Yes, you can right-click on any resource within eclipse and choose 'properties'. Within the 'resource' tab there is a check box for 'derived'.

  3. Hi Sven,

    Thanks for the examples. Could I use Xtend for code generation ? I want to generate C-code from my java object model. Also i want to call the generator from Java. Would you recommend Xtend for this task or traditional code generators like JET would suit better ?

    Is there some example where I could refer for more examples based on Xtend?


    1. Of course! With its template expressions and dispatch methods Xtend is well suited for doing code generation.

  4. Hello Sven,

    Does Xtend enable better M2M transformations ?

    Background: I am dealing with extensive M2M transformations in my project. I am evaluating Xtend to make the task more efficient (Currently its java based conversion). But some of my Models are not based on EMF [they dont have a .ecore] I understand that Xtend gives a better model traversing mechanisms directly based on the metamodel.

    Do you think adopting Xtend for my case would be beneficial ?


    1. Yes, Xtend has some features to offer which are quite useful for m2m transformations.
      Not using EMF is no problem as Xtend works with plain Java Objects (just like Java itself does).
      In fact it doesn't know a thing about EMF and treats EMF objects just like an other Java object.

    2. Thank you for the quick reply :)

    3. Thank you Sven.

      I have now begun with Xtend based transformation, Hoping for the best. Do you know of any extensive tutorials on M2M transformations using Xtend2 ?

      Also is there any way I could call the Xtend Script from my java ?

      Background: I have written an XTend based M2M transformer which takes a rather simple EMF model and does a basic transformation into another one. Now I want to invoke the transformer from my java code. Is there a way do this ?

    4. Sorry for being naive in my previous reply. I forgot I have the generated java code with me so I think can at anytime like any java methods. :)