Home

Xtend lambda expression type definitions with and without syntactic sugar

The usual or shortest way to write a lambda expression in Xtend is to leave out the type information. Xtend2 works with local type inference so there is no need to explicitly write down the type.

val myFunction = [String a, String b | a.compareTo(b)]

There is a special short syntax for lamda expression type definitions in Xtend2 that looks like the following.

val (String, String)=>int myFunction = [String a, String b | a.compareTo(b)]

The most verbose way to write a lambda expression in Xtend2 is the following. It has the long full type definition and also the optional round brakets around the expression itself.

import org.eclipse.xtext.xbase.lib.Functions.Function2

val Function2<String,String,Integer> myFunction = ([String a, String b | a.compareTo(b)])

In the last and most verbose example one can see that the local type inference is really helpful as the first example is a lot easier to read.

The examples were written and tested with Xtend 2.2.1

References


Date published:
Date modified: 2012-09-05
Keywords: Languages, closures, lambda expressions, Xtend