What's Xtend?
● Hosted by Eclipse Foundation● Based on Xtext● Similar syntax to Scala or Kotlin● Compiled to Java source code (not class file)● Eclipse and IntelliJ support
Tool support
● IDE Plugins○ Eclipse○ IntelliJ IDEA (not work with IntelliJ 2017.1.2?)
● Build tools○ Maven○ Gradle
About Xtext
● Framework for development of languages● Define language using BNF like DSL ● Generate toolkit like parser or editor from it
Language Definition
Parser
Compiler
Linker
Type checker
Editor
Generate
Xtext Summit
● In EclipseCon 2017 France● June 20-21 (2days!!)● One of the hottest projects in Eclipse.org
Basic syntax
● def / var / val● () in method invocation is optional● return and return type are optional● null-safe call: myRef?.doStuff● All exceptions are treated as runtime● if, try, switch and block are expression
(for and while are void, unfortunately)
Extension methods
class HelloWorld {def static print(String s){
println(s)}def static void main(String[] args){
"Hello World".print()}
}
Extension methods
class StringExtensions {def print(String s){ ... }
}
class HelloWorld {static extension StringExtensions extensions = …
def static void main(String[] args){"Hello World".print()
}}
Import extension methods defined in other class
Lambda expressions
def static void main(String[] args){val list = Arrays.asList("Kotlin", "Java", "Xtend")
Collections.sort(list) [ a, b | a.length - b.length]
println(list)}
Translated to lambda
Dispatch methods
def static dispatch printType(Number x) { "it's a number"
}def static dispatch printType(Integer x) {
"it's an int" }
def static void main(String[] args){println(10.printType)
}
Switch expressions
def length(Object x) {switch x {
String case x.length > 0 : x.lengthList<?> : x.sizedefault : -1
}}
Type guard and case guard are available
Template expressions
def someHTML(String content) ''' <html> <body> «content» </body> </html>'''
Three single quote!
Embed variables as «...»
Active annotations (Macro)
@Data class User {long userIdString userNameString email
}
Generate code triggered by annotation
Xtend...
● has simple and powerful syntax.● has excellent Eclipse support.● covers Web, GUI and Android.● offers 100% Java interop.