PROPOSAL: Conditional Statement
Matt Mastracci
matthew at mastracci.com
Sun Mar 29 19:11:16 PDT 2009
Neal,
There are a number of small benefits, IMHO:
1. The conditional syntax takes up a single line for simple choice,
vs. 3 or 5 lines (depending on whether your IDE inserts braces for
simple if statements) using the normal if syntax. This makes it
easier to scan source, but doesn't decrease readability.
2. When changing the return value of a function that uses
conditionals from int to void, you currently have to expand the
conditional out or assign the value to an unused temporary:
public int foo() {
return flag ? bar() : baz();
}
Old refactorings:
public void foo() {
if (flag) {
bar();
} else {
baz();
}
}
@SuppressWarnings("unused")
public void foo() {
int unused = flag ? bar() : baz();
}
New refactoring:
public void foo() {
flag ? bar() : baz();
}
3. Consistency with the conditional expression. There's no way to
take advantage of the conditional operator for functions with void
return types. By creating a construct where this is possible, it
makes the language more consistent.
On 29-Mar-09, at 7:54 PM, Neal Gafter wrote:
> I don't understand the benefit over the existing if statement.
>
> On Sun, Mar 29, 2009 at 2:41 PM, Matt Mastracci
> <matthew at mastracci.com> wrote:
>> 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