Proposal: Block Expressions for Java

Neal Gafter neal at
Fri Feb 27 21:21:31 PST 2009

 Block Expressions for Java*AUTHOR(S):*Neal Gafter
*OVERVIEW*FEATURE SUMMARY:A parenthesized expression can now contain
additional statements, for example declarations of temporary local variables
to avoid recomputing a value used only within the expression.  This feature
is especially useful in machine-generated code.
MAJOR ADVANTAGE:Allows the declaration of local temporary variables closer
to the point of use.  Simplifies programs that generate Java code, as
temporary variables that are used in only a single expression can be
declared with the expression.
 MAJOR BENEFIT:Simplifies some patterns of code (see above).  Makes it
easier to write expression-oriented code even though many APIs are
statement- or side-effect-oriented.  Allows temporaries to be declared
closer to the point of use.
 MAJOR DISADVANTAGE:Slightly more complex language specification.  Like most
other language features, can be overused to the detriment of the readability
of the code.
ALTERNATIVES:In many cases, there are alternate though awkward ways of
writing the code, such as introducing small helper functions that are used
only once or introducing temporary variables.
EXAMPLES SIMPLE EXAMPLE:*double pi2 = (double pi = Math.PI ; pi*pi)**; // pi
 ADVANCED EXAMPLE:*public static final Map<Integer,Integer> primes = (
    Map<Integer,Integer> t = new HashMap<Integer,Integer>();
    t.put(1, 2); t.put(2, 3); t.put(3, 5); t.put(4, 7);
DETAILS SPECIFICATION:*Grammar*: the grammar for a parenthesized expression
[JLS 15.8.5<>]
is changed to:

 **(** **BlockStatementsopt **Expression **)**

Note that the existing syntax for a parenthesized expression is a special
case of this.

*Meaning of Expressions*: The specification for a parenthesized expression
should be modified to describe its new execution semantics: The block
statements (if any) are executed in sequence, from left to right, exactly as
in a block statement.  The result of the parenthesized expression is the
result of evaluating its subexpression.

*Definite Assignment*: The definite assignment rules for this construct are
almost identical to that for the block statement.  The definite assignment
state before the subexpression is the definite assignment state following
the last block statement.

*Exception Checking*: A parenthesized expression can throw exception type E
if any statement or expression immediately contained in it can throw E.

*Scoping*: Local variables and classes declared by an immediately contained
statement is in scope until the end of the parenthesized expression.

*Meaning of Statements*: No changes.

*Type System*: No changes.
 COMPILATION:Compilation is straightforward, with one minor exception (no
pun intended).  The compiler must arrange the generated code such that the
stack is empty at the beginning of any try statement that can complete
normally when the try statement is embedded within a parenthesized
expression.  That is because the VM empties the stack when exceptions are
handled.  This can be accommodated by spilling values from the stack into VM
 TESTING:Since the language change is completely local, it can be tested by
exercising each of its interactions described above.
 LIBRARY SUPPORT:None required.
 REFLECTIVE APIS:No changes required.
 OTHER CHANGES:It would be desirable, at the same time that this change is
made, to update the non-public Tree API that can be used with APT to express
the syntax extension.
 MIGRATION:None required.
 EXISTING PROGRAMS:No effect on existing programs.  This is a pure
 URL FOR PROTOTYPE (optional):None.

More information about the coin-dev mailing list