Alternative syntax for closures

Neal Gafter neal at
Mon Jul 7 00:07:16 PDT 2008

One idea we tossed around a few months ago, without any real conclusion, was
to replace the curly braces with parens. Things have changed a bit since
then; I'd now use a dash-greater instead of equals-greater.  I think we
could consider using the parens for restricted closures (for those of you
who haven't been paying attention, restricted closures can't use return,
break, or continue to a target outside the closure) and curly braces for the
unrestricted form.

( *FormalParameterDecls**opt* *—**>* *BlockStatementsopt **Expression**opt**
( *TypeList**opt* *—**>* *Type* *ThrowsClause**opt* )

{ *FormalParameterDecls**opt* *—**>* *BlockStatementsopt **Expression**opt**
{ *TypeList**opt* *—**>* *Type* *ThrowsClause**opt* }

For example (this is *not* intended to be a motivational example):

* (int, int **—**> int) plus = (int x, int y **—**> x+y);*
*{ Answer —**> void } block = { **Answer answer —**>
System.out.println(answer); };*

The parens instead of curly braces emphasize to the reader that it is an
expression closure, which is one of the principal use cases for the
restricted form.

Without an active working group, I don't expect to make any major syntax
changes in the prototype, but I wanted to get this out there for you to
think about.

-------------- next part --------------
An HTML attachment was scrubbed...

More information about the closures-dev mailing list