PROPOSAL: Conditional Statement
Matt Mastracci
matthew at mastracci.com
Sun Mar 29 14:41:10 PDT 2009
Conditional Statement
=====================
Richly formatted version available at:
http://docs.google.com/Doc?id=dgthwhwr_2hs2hknhf
AUTHOR(S): Matt Mastracci
OVERVIEW
FEATURE SUMMARY: Allows an analogue of the conditional expression to
exist at the statement level, with relaxed restrictions on the type of
its second and third operands.
MAJOR ADVANTAGE: Ability to use conditional operator for choosing
between two statement-level expressions (ie: two different method
invocations).
MAJOR BENEFIT: Simplifies code that executes one of two expression
statements given a boolean input.
MAJOR DISADVANTAGE: Turns the conditional operator into two different
constructs in the Java language with different rules.
ALTERNATIVES: Continue using if/else (this feature is sugar only).
See http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25
for the definition of the original conditional expression.
EXAMPLES
// even() and odd() are defined as "public void"
public void onEvenOrOdd(int i) {
(i % 1 == 0) ? even() : odd();
}
// Two methods with different return types, valid
public void oneOfTwoMethodsWithDifferentReturnTypes(boolean b) {
b ? methodReturningObject() : methodReturningVoid();
}
Examples that fail to compile:
// Invalid because operands are not valid StatementExpressions
public void invalid1() {
b ? 1 : 2;
}
// Invalid because third operand is not a valid StatementExpression
public void invalid2() {
b ? method() : null;
}
DETAILS
SPECIFICATION: This change requires two modifications to the Java
Language Specification:
1. A ConditionalStatement is added to the specification. A
ConditionalStatement takes a boolean expression as its first operand
and two StatementExpressions as the second (if-true) and third (if-
false) operands:
ConditionalStatement
Expression ? ConditionalStatementOperand :
ConditionalStatementOperand
ConditionalStatementOperand
( ConditionalStatementOperand )
StatementExpression
There are no restrictions on the output type of the
StatementExpression. Any valid statement expression today is valid as
an operand of the conditional statement.
The conditional statement is right-associative like the conditional
expression.
2. The definition of StatementExpression is changed to allow the
conditional operator statement as one of its alternatives (this might
be better to add to the StatementWithoutTrailingSubstatement block
instead):
StatementExpression:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
+ ConditionalStatement
COMPILATION: The ConditionalStatement may always be desugared to a
standard if/else construct:
boolean-expression ? statement-expression-1 : statement-expression-2
desugars to:
if (boolean-expression)
statement-expression-1;
else
statement-expression-2;
TESTING: Add a conditional statement to the files tested during
complation. Test that a conditional statement fails to compile when
operands are not valid StatementExpressions.
LIBRARY SUPPORT: None.
REFLECTIVE APIS: No changes.
OTHER CHANGES: No changes.
MIGRATION:
An IDE may offer to automatically simplify if/else constructs with two
expression statements:
if (condition) {
expression-statement-1;
} else {
expression-statement-2;
}
to:
condition ? expression-statement-1 : expression-statement-2;
COMPATIBILITY
BREAKING CHANGES: None
EXISTING PROGRAMS: No issues.
REFERENCES
EXISTING BUGS: None found.
More information about the coin-dev
mailing list