BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Java Lambda Syntax based on C#, Scala

Java Lambda Syntax based on C#, Scala

Leia em Português

This item in japanese

One of the most anticipated new features of Java 8 is the introduction of Lambdas, which are anonymous functions that can be passed around like data in arguments and variables. However, the syntax for these wasn't widely decided, with polls asking for input on what would be most familiar to others.

A recent posting on the lambda-dev mailing list announced the conclusion that the Java Lambda syntax will be based on C# syntax. This is already widely known and used (as C# had delegates back in 1.0, support for delegate functions in 2.0 and lambda expressions in 3.0).

Other alternatives were available (such as BGGA) but the response to the survey suggested that there wasn't a huge advantage of one over the other; in addition, the pre-existing experiences of those using C# and Scala mean that there are likely to be many more programmers familiar with the general type of syntax. (The key difference between the C# and the Scala style of anonymous function is with how arguments are typed; C# uses “int foo” whilst Scala uses “foo: int” – a difference that will be fairly easy to move between. Java will adopt C#'s style of “int foo” in deference to the way types are represented in Java.)

Despite extensive searching, there was no clear winner among the alternatives (each form had some good aspects and some really not very good aspects, and there was no form that was clearly better than the others). So, we felt that it was better to choose something that has already been shown to work well in the two languages that are most like Java – C# and Scala – rather than to invent something new.

Both expressions and blocks will be permitted. An expression is represented without the curly braces {} and will, when evaluated, return the result. Blocks of statements are surrounded with {} and will not return a result unless the return keyword is used. The examples given in the mail are:

x => x + 1
(x) => x + 1
(int x) => x + 1
(int x, int y) => x + y
(x, y) => x + y
(x, y) => { System.out.printf("%d + %d = %d%n", x, y, x+y); }
() => { System.out.println("I am a Runnable"); }

One key advantage of the lambda expressions is that they will infer the types of the arguments if not specified. In some cases, the compiler may not be able to infer the correct types (especially in the case of overloaded operators; the “(x,y) => x+y” is highly unlikely to compile if Java doesn't know whether the x and y are long or double (or even Object). In most cases the type inference engine is likely to generate the correct code, although if further assistance is needed then the developer can always explicitly add the typing information.

A compiler which understands the new syntax will be made available in the near future for experimentation purposes.

Rate this Article

Adoption
Style

BT