Xtend Documentation:
- Dispatch: http://www.eclipse.org/xtend/documentation.html#polymorphicDispatch
- Switch: http://www.eclipse.org/xtend/documentation.html#switchExpression
Dispatch implementation:
class PrettyPrinterXtend1 {
public static val DecimalFormat INT_FORMAT = new DecimalFormat("00");
def public static dispatch String prettyPrint(Integer intValue) {
return "Integer: " + INT_FORMAT.format(intValue);
}
def public static dispatch String prettyPrint(Rectangle it) {
if (width == height) {
return "Square with side=" + width;
}
"Rect with width=" + width + " and height=" + height;
}
def public static dispatch String prettyPrint(Void object) {
"<null>"
}
def public static dispatch String prettyPrint(Object object) {
"Object: " + object;
}
}
Switch implementation:
class PrettyPrinterXtend2 {
public static val DecimalFormat INT_FORMAT = new DecimalFormat("00");
def public static String prettyPrint(Object object) {
switch (object) {
Integer:
"Integer: " + INT_FORMAT.format(object)
Rectangle case object.width == object.height:
"Square with side=" + object.width
Rectangle:
"Rect with width=" + object.width + " and height=" + object.height
case null:
"<null>"
default:
"Object: " + object
}
}
}