Proposal: Block Expressions for Java
neal at gafter.com
Fri Feb 27 21:30:24 PST 2009
[Resending in plain text]
Block Expressions for Java
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.
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.
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.
Slightly more complex language specification. Like most other
language features, can be overused to the detriment of the readability
of the code.
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.
double pi2 = (double pi = Math.PI ; pi*pi); // pi squared
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);
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
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
Scoping: Local variables and classes declared by an immediately
contained statement is in scope until the end of the parenthesized
Meaning of Statements: No changes.
Type System: No changes.
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 locals.
Since the language change is completely local, it can be tested by
exercising each of its interactions described above.
No changes required.
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.
No breaking changes.
No effect on existing programs. This is a pure extension.
URL FOR PROTOTYPE (optional):
More information about the coin-dev