Saturday, March 27, 2010

Xtext won Eclipse Community Award!

What a great community you are! Thank you so much for giving us this kind of support. We are all incredibly proud about the fact that Xtext has won the Eclipse Community Award 2010.


I also want to take the opportunity to say thank you to the team once more. Especially to Dennis Huebner who couldn't make it to this year's EclipseCon. Itemis and in particular our bosses Wolfgang Neuhaus and Jens Wagener also deserve a big thank you: If they hadn't believed in my vision two years ago and still do, there were no Xtext and we were all working on different things and hadn't had the chance to combine our forces to create our favorite programming language and IDE development framework.

I already put the award logo Lynn sent me on our website. See how nice it fits into the Xtext-Nova theme.

EclipseCon 2010 was so much fun. I'm still in San Francisco but am already looking forward to next year's issue of my favorite conference. Thank you for all the support!

Monday, March 22, 2010

EclipseCon 2010

I'm sitting in my room at the Hyatt hotel in Santa Clara, California. It's 6 am and I'm glad I could sleep til 5 am at least. Seems that the 7h bike trip we did yesterday did its job (we hoped that being outside most of the day would get us in tune with the local time zone). I only wished the saddle hadn't been so hard....

Today, the conference starts and I'm currently looking at what sessions I'ld like to attend.

Monday
Monday starts with the introduction of the e4-Rover Mars Challenge. I bet you've already heard of it. Right after that Jan, Moritz and Heiko give a 3 hour tutorial called "Xtext meets e4". They will show how one can model a workbench in a textual way. They have worked hard on preparing the workshop material and I'm curious to see how the audience like what they have. After lunch the shorter presentations begin. I'm going to see what Stephan and Kenn think the twenty modeling things are and am looking forward to the b3 talk. After that Peter has his session on Xpand just before Heiko presents on how one can apply MDSD to develop IPhone apps. Also I want to note Jan's talk on combining GMF and Xtext.
In the evening I have a modeling panel and after that there's the community awards ceremony. Xtext is finalist in the category "most innovative project". How cool is that? :-)

Tuesday
In the morning I'll be out on another bicycle trip. After that I'll have to practice my presentations for the afternoon (yes I practice and I always hope nobody watches me while I'm doing so :-)).
After lunch there are a couple of talks I find interesting. Kai presents on CSS in e4 and Berni does his penalty shoot out of textual modeling tools (didn't know there are other than Xtext ;-)).
I'm also interested in what's new in CDT. Not because I'm a big fan of C/C++, but I'm interested in Eclipse-based IDE development. At 2 pm it's showtime for Sebastian and me. We'll demo the coolest IDE features you get when developing a language in Xtext. And right after that there's the Modeling Runway, where a couple of modeling projects present their projects (5 min each).

Later that day I'll likely attend the Graphiti talk. Let's see if it is any better than GMF. I think I'll have a break after that in order to read through Wayne's revised Eclipse Development Process in order to be prepared for the Development at Eclipse panel I am attending.

Wednesday
I am not yet decided whether to go to "Modular Architecture from Top to Bottom" or "API Design and Evolution" in the morning. Both seem very interesting, but I guess I'll have more fun asking controversial questions in the API talk :-). In the afternoon I probably will visit the Groovy IDE talk. The new version of the Eclipse plugin looks great. Especially the tight integration with JDT is pretty cool. But I also might go out for another trip, since the weather is forecast to be great. If not I'll go to see what cool features Eike and his team brought to CDO lately. In the evening Sebastian will present the new version of MWE2. It's implemented in Xtext and we're very proud of the result and how easy it was to implement it using Xtext.

Thursday
For me the day certainly starts with "Understanding and Using Git at Eclipse". Actually we're still using CVS and I personally am ok with it because of the great tooling Eclipse provides. The only problem we have is its slowness. Synchronizing my development workspace sometimes takes 15 min. So we plan to switch to Git right after Helios.
In the afternoon the talk about Dependency Injection got my attention. I love DI, though I am also a bit concerned about how DI is used in e4. I'll make sure to have some discussions with the e4 folks about my concerns. I'll have to bring my bicycle back to the store in the early evening, so I won't be able to attend any other talks on Thursday.

These are just my personal selection of a great variety of interesting talks you can attend at this year's EclipseCon. All in all it looks like a very diverse program and I'm looking forward to four interesting days full of great talks and discussions. I especially like the trend towards shorter presentations because I expect them to be better prepared and more focused.
....ups, it's already 8:17 am (I've had a great breakfast in the meantime) and I am missing the e4-Rover Mars Challenge. I'll have to go now. See you!

Thursday, March 18, 2010

Multiple dispatch (and poor men's patter matching) in Java

There are times where you do not want to or simply cannot add a certain aspect of behavior to your domain model's class hierarchy.
Most applications are layered but share the same domain model (the times where people translate state into layer-specific representations are hopefully gone). Sharing the same domain model classes between different layers is convenient and effective. However if you do that, your domain model tends to become a so called anemic domain model, that is you do not add the behavior to your classes but hold it externally in order to keep the domain model layer independent. This ensures that your domain model only contains code which belongs to the domain and not to a technical layer. That's ok because you do not want to have UI-specific or database-specific code in your domain model if you pass it through all layers.

The problem is that you cannot leverage Java's polymorphism anymore. As a result you either write lengthy if-else cascades in order to dispatch between different types or you use the visitor pattern, which is some kind of external polymorphism support. Well actually the old visitor pattern is not that much external but very invasive. You have to prepare your domain model to be able to accept visitors. That's not always possible and also a lot of work.

Other languages support features to solve this problem. One is pattern matching as known from functional languages which also has made its way to object oriented languages. It's nice and powerful but not available in Java. :-(
Another feature is "multiple dispatch". Languages supporting multiple dispatch (aka "multimethod") decide which method to invoke based on the actual runtime type of the passed arguments. Java in contrast links the method at compile time based on the static types.

This is why in Java the following would print "Hello Fruit":
print((Fruit) new Banana());

static void print(Fruit f) {
sysout("Hello Fruit");
}

static void print(Banana b) {
sysout("Hello Banana");
}
If Java would support multiple dispatch it would print "Hello Banana", because at runtime the passed argument is an instance of Banana.

In Xtext we have "added" multiple dispatch to Java (we call it polymorphic dispatch). For instance a label provider is implemented like this:

public class MyLabelProvider extends AbstractDeclarativeLabelProvider {

String text(Banana b) {
return "yellow fruit";
}

String text(Apple b) {
return "keeps the doctor away";
}
}
The framework just calls ILabelProvider.getText(Object element) and the implementation of this method dispatches to the best matching label(Object) method. So if you are interested how it works, have a look at the PolymorphicDispatcher class. Other examples where we use this technique are validation, scoping, qualified name provider, content assist, outline view, etc.

This stuff is very useful for defining such kinds of APIs because it allows us to define nice defaults while the user can add specific behavior just by adding the corresponding methods.

But what if you are in the middle of some implementation and only need to make some decisions based on the type? Write code like this?

Fruit fruit = new Banana();
String result=null;

if (fruit instanceof Banana) {
Banana b = (Banana) fruit;
result = // do stuff with b;

} else if (fruit instanceof Apple) {
Apple a = (Apple) fruit;
result = // do stuff with a;

} else if (fruit instanceof Peach) {
Peach p = (Peach) fruit;
result = // do stuff with p;
}

System.out.println(result);

You might get into trouble with these guys.
How about this one?:

System.out.println(
new Match() {

String is(Banana b) {
return // do stuff with b;
}

String is(Apple a) {
return // do stuff with a;
}

String is(Peach p) {
return // do stuff with p;
}

}.apply(new Banana()));

It's only a bit shorter, but I like it much better because it is functional and more declarative. That is I don't have to declare variables and do side-effects within the different if statements. Also this works for multiple parameters as well.

P.S.: If your domain model is implemented in EMF you get a so called Switch-class generated, which acts similar. So if you only dispatch within that hierarchy and you only need one argument, this works great as well.