Sunday, November 21, 2010

Compact anonymous inner classes in Scala

Scala programming language has a much more compact syntax for anonymous inner classes.

This code in Java:

import com.vaadin.ui.*;

home.addComponent(new Button("Manage Users", new Button.ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
panel.setContent(userManagementLayout);
}
}));

becomes this in Scala:

import com.vaadin.ui._

home.addComponent(new Button("Manage Users", (event: Button#ClickEvent) =>
    panel.setContent(userManagementLayout);
)));

No comments:

Post a Comment