Xtend statically typed Java-like language general purpose a better Java with less “noise” interoperable with Java possible to reuse Java libraries Java type system generics annotations more concise syntax type inference extension methods dispatch methods multiline template expressions Xtend programs translated to Java Xtend language implemented in Xtext can be used independently from Xtext
Example of Xtend code less verbose than Java optional semicolons methods are public by default
Xtend Methods, fields and variables no statements everything is an expression last expression in body is the return expressionmethod return type can be omitted if it can be inferred types must always be specifiedmethod params implicitly final (immutable) method definition: either def or override several classes can be defined in one file private by defaultfield
Xtend Fields and variables val for final var for non-final == === compares the values of objects maps the operator to method equals object identifier equality in Java sense final fields and variables must be initialized o.getName()getters and setters o.name() o.setName(...) o.name = ...
Xtend Extension methods UFCS Uniform function call syntax as in programming language D f(x) x.f() x.f f is not defined in MyType add new methods to existing types idea similar to class helpers as if f were a method of class MyType MyType x ... as in Delphi omitted parentheses
Xtend Extension methods extension field of type T in class A – methods of T become extension methods in A extension variable – in code block extension parameter – in method body name is optional methods defined in Xtend class can be used as extension methods
Xtend Implicit variable it class ItExamples { def func1(String it) { return toLowerCase // it.toLowerCase } def func2(String s) { var it = s toLowerCase // it.toLowerCase } } all members of it are implicitly available similar to this all members of this are available in an instance method
Xtend Enhanced switch expressions def String switchExample(Entity e, Entity specialEntity) { switch e { case e.name.length > 0: “has a name” case e.superType != null: “inherits some entity” case specialEntity: “equals to special entity” default: “nothing” } } switch value – any object reference Boolean – case matches if true not Boolean – compare using equals Boolean – case matches if true
Xtend Enhanced switch expressions def toString(FieldType fieldType) { switch (fieldType) { BasicType : fieldType.typeName EntityType : fieldType.entity.name } } only the selected case is executed in Java falls though all matching cases no need for explicit break instructions does not support break statementsXtend Type guards cases are evaluated in the specification order case matches only if switch value conforms to type
Xtend Polymorphic method invocation method overloading static mechanism selection of the specific method according to static type of arguments class hierarchy: base class AnyType two classes NumberType, TextType method typeToString defined in another class static overloading would use AnyType NumberType desired – but only known at runtime multiple dispatch method selected according to runtime type of arguments
Xtend Lambda expressions val l = [String s, int i | s + i] println(l.apply(“s”, 10)) val (String, int)=>String l = [String s, int i | s + i] val (String, int)=>String l = [s, i | s + i] def execute( (String, int)=>String f ) { f.apply(“s”, 10) } execute([s, i | s + i]) val list = newArrayList(“John”, “Mary”, “David”, “Alice”) Collections.sort(list, [arg0, arg1 | arg0.compareToIgnoreCase(arg1)]) Collections.sort(list) [arg0, arg1 | arg0.compareToIgnoreCase(arg1)] mystrings.findFirst[s | s.startsWith(“T”)] mystrings.findFirst[it.startsWith(“T”)] mystrings.findFirst[startsWith(“T”)] anonymous function can be stored in a variable can be passed to methods types for lambda expressions types can be omitted if can be inferred SAM (Single Abstract Method) known in Java as functional interfaces
Xtend With operator return eINSTANCE.createPerson => [ name = “John” surname = “Smith” ] val person = eINSTANCE.createPerson person.name = “John” person.surname = “Smith” return person binds an object to the scope of a lambda expression result of with operator is the object itself lambda expression with single parameter binary operator=> expressionleft operand right operand it can be omitted executes lambda with left-hand side as the argument result left operand after applying lambda

Xtend Programming Language

  • 1.
    Xtend statically typed Java-like languagegeneral purpose a better Java with less “noise” interoperable with Java possible to reuse Java libraries Java type system generics annotations more concise syntax type inference extension methods dispatch methods multiline template expressions Xtend programs translated to Java Xtend language implemented in Xtext can be used independently from Xtext
  • 2.
    Example of Xtendcode less verbose than Java optional semicolons methods are public by default
  • 3.
    Xtend Methods, fieldsand variables no statements everything is an expression last expression in body is the return expressionmethod return type can be omitted if it can be inferred types must always be specifiedmethod params implicitly final (immutable) method definition: either def or override several classes can be defined in one file private by defaultfield
  • 4.
    Xtend Fields andvariables val for final var for non-final == === compares the values of objects maps the operator to method equals object identifier equality in Java sense final fields and variables must be initialized o.getName()getters and setters o.name() o.setName(...) o.name = ...
  • 5.
    Xtend Extension methods UFCSUniform function call syntax as in programming language D f(x) x.f() x.f f is not defined in MyType add new methods to existing types idea similar to class helpers as if f were a method of class MyType MyType x ... as in Delphi omitted parentheses
  • 6.
    Xtend Extension methods extensionfield of type T in class A – methods of T become extension methods in A extension variable – in code block extension parameter – in method body name is optional methods defined in Xtend class can be used as extension methods
  • 7.
    Xtend Implicit variableit class ItExamples { def func1(String it) { return toLowerCase // it.toLowerCase } def func2(String s) { var it = s toLowerCase // it.toLowerCase } } all members of it are implicitly available similar to this all members of this are available in an instance method
  • 8.
    Xtend Enhanced switchexpressions def String switchExample(Entity e, Entity specialEntity) { switch e { case e.name.length > 0: “has a name” case e.superType != null: “inherits some entity” case specialEntity: “equals to special entity” default: “nothing” } } switch value – any object reference Boolean – case matches if true not Boolean – compare using equals Boolean – case matches if true
  • 9.
    Xtend Enhanced switchexpressions def toString(FieldType fieldType) { switch (fieldType) { BasicType : fieldType.typeName EntityType : fieldType.entity.name } } only the selected case is executed in Java falls though all matching cases no need for explicit break instructions does not support break statementsXtend Type guards cases are evaluated in the specification order case matches only if switch value conforms to type
  • 10.
    Xtend Polymorphic methodinvocation method overloading static mechanism selection of the specific method according to static type of arguments class hierarchy: base class AnyType two classes NumberType, TextType method typeToString defined in another class static overloading would use AnyType NumberType desired – but only known at runtime multiple dispatch method selected according to runtime type of arguments
  • 11.
    Xtend Lambda expressions vall = [String s, int i | s + i] println(l.apply(“s”, 10)) val (String, int)=>String l = [String s, int i | s + i] val (String, int)=>String l = [s, i | s + i] def execute( (String, int)=>String f ) { f.apply(“s”, 10) } execute([s, i | s + i]) val list = newArrayList(“John”, “Mary”, “David”, “Alice”) Collections.sort(list, [arg0, arg1 | arg0.compareToIgnoreCase(arg1)]) Collections.sort(list) [arg0, arg1 | arg0.compareToIgnoreCase(arg1)] mystrings.findFirst[s | s.startsWith(“T”)] mystrings.findFirst[it.startsWith(“T”)] mystrings.findFirst[startsWith(“T”)] anonymous function can be stored in a variable can be passed to methods types for lambda expressions types can be omitted if can be inferred SAM (Single Abstract Method) known in Java as functional interfaces
  • 12.
    Xtend With operator returneINSTANCE.createPerson => [ name = “John” surname = “Smith” ] val person = eINSTANCE.createPerson person.name = “John” person.surname = “Smith” return person binds an object to the scope of a lambda expression result of with operator is the object itself lambda expression with single parameter binary operator=> expressionleft operand right operand it can be omitted executes lambda with left-hand side as the argument result left operand after applying lambda