# Java Lambda Syntax based on C#, Scala

| by Alex Blewitt 4 Followers on Sep 09, 2011. Estimated reading time: 2 minutes |

A note to our readers: You asked so we have developed a set of features that allow you to reduce the noise: you can get email and web notifications for topics you are interested in. Learn more about our new features.

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.

Style

## Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

## Get the most out of the InfoQ experience.

### Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Juhu!!!

Great! Java builds on the shoulders of giants (C## and the little giant Scala). :-)

Amazing - Java-ites acknowledging other languages

Maybe they can also acknowledge the debt owed to Smalltalk while they are at it.

Curiously, a Smalltalk like language may be coming back as "Dart" from Google:

gotocon.com/aarhus-2011/presentation/Opening%20...

Re: Amazing - Java-ites acknowledging other languages

We seriously have to wonder if this "Dart" thing is for real. I say tread with caution.

Re: Amazing - Java-ites acknowledging other languages

(coming from the smalltalk world, I know that the Self VM had wonderful things and the blog of Gilad Bracha about Newspeak always have interesting posts about programming languages and modularity)

Re: Amazing - Java-ites acknowledging other languages

Actually, the Newspeak-to-ECMAScript compiler has been "almost finished" for quite a while now. I suspect Dart is related to that, maybe a slightly modified version or a subset of Newspeak that is easier to compile to ECMAScript than the extremely dynamic and reflective "proper" Newspeak.

Newspeak has been designed from day 1 to be a web programming language, or rather to be the programming language for the next evolution of the web (one could even say that Newspeak was designed to be the next evolution of the web), so it would make sense. Even if you don't actually use all the cool revolutionary features of the full Newspeak system (which would be quite hard anyway, since they haven't been implemented yet :-) ), Newspeak is still a very nice object-oriented dynamic language with software-engineering capabilities far beyond those of your typical "scripting" languages (including Smalltalk), e.g. a proper module system. Plus, a very nice actor system (and web programming is basically always distributed, asynchronous programming which is exactly what actors do well) and powerful pattern matching (which, again, is a big part of what many web apps do).
Close

#### by

on

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

5 Discuss

Login to InfoQ to interact with what matters most to you.