xtend - better java with -less- noise
Post on 06-Apr-2017
225 Views
Preview:
TRANSCRIPT
Agenda
1. Xtend introduction2. Active Annotations3. Extension methods 4. With operator, Lambda expressions5. Switch expressions
Xtend ?
● Xtend is a statically typed programming language● It is an “extension” to Java and NOT a replacement● It fully supports all the Java idioms● It allows usage of existing Java API in much nicer way without any
interoperability issues● Provides additional features like - Type Inference, Extension methods,
Lambda expressions, Active annotations...● Latest release - Xtend 2.9.0.
Xtend ?
● It is implemented in Xtext and is a proof of concept of how involved a language implemented in Xtext can be.
Active Annotations
● Code written using Xtend gets translated to Java● The translation process can be “altered” using Active annotations● Mainly used to generate boilerplate Java code; that otherwise is coded
manually.
Active annotation (Syntax)
● Annotation which is itself annotated with Active @Active(SWTBotProcessor)annotation SWTBot { }
● @Active takes a Annotation Processor as the parameter● It can either be declared in Java or Xtend.
Existing Active annotation
● org.eclipse.xtend.lib.annotations○ @Accessor - Adds getter and or setter methods for your fields
○ @Data - Turns an annotated class into Value object class. Generates - final fields, getter methods, constructor with parameters, equals, hashCode and toString methods
○ @FinalFieldsConstructor - Creates a constructor that takes a parameter for each final field of a class.
○ @EqualsHashCode - Creates default implementation for equals and hashCode.
Extension methods
● Allows you to add additional methods to an existing type without modification"hello".toFirstUpper() // calls StringExtensions.toFirstUpper("hello")
● Based on a simple syntactic trick: The method is called with the first argument as its receiver entity.save(comment) // persistenceManager.save(entity, comment);
● Resulting code is much more readable
Runtime library of extension methods
● org.eclipse.xtext.xbase.lib.ObjectExtensions● IterableExtensions● MapExtensions ● ListExtensions ● CollectionExtensions ● BooleanExtensions ● IntegerExtensions ● FunctionExtensions.
with operator (=>/double-arrow operator)
● Allows you to bind objects to the scope of lambda expression so that you can perform some operations on itnew Button(parent, style) => [text = “Hello World” enabled = false]
● The operator => passes the left expression as a parameter to the lambda on the right. The result of this operation is the object itself.
Java 8 vs. Xtend (subtle differences)shapes.forEach(s -> { s.setColor(RED);});
1. Represented using arrow ->2. The ( ) braces are mandatory. Lambda
needs to be passed inside ( ) braces3. The body of the lambda needs to be inside
curly braces { } and semicolons are mandatory
4. Parameter names (s) is mandatory.
shapes.forEach[color = RED]
1. Represented using square brackets [ ]2. Not mandatory. However, you can use the
( ) braces3. Lambda is a block expressions. No
semicolons4. Parameter name is optional. Implicit
variable “it” is used.
Java 8 vs. Xtend (type inference, less verbose)
Java :List<Album> sortedFavs = albums.stream()
.filter(a -> a.tracks.anyMatch(t -> (t.rating >= 4)))
.sorted(comparing(a -> a.name))
.into(new ArrayList<>());
Xtend :val sortedFavs = albums.stream
.filter[tracks.anyMatch[rating >= 4]]
.sorted(comparing[name])
.into(newArrayList)
Switch expressions
● Not limited to certain values. It can be used for any object reference● For case expression of type boolean, the case matches if the expression
evaluates to true, for other types main expression is compared using Object.equals (Object)
References
Documentationhttps://eclipse.org/xtend/documentation/Presentation’shttp://goo.gl/AlWDkBBlog’shttps://oehme.github.io/http://blog.efftinge.de/2012/12/java-8-vs-xtend.htmlSource codehttps://github.com/oehme/xtend-contribhttps://github.com/joergreichert/ActiveAnnotationsExamples
top related