rfc:poll_switch_expression

Poll: Switch expression

Introduction

Before proceeding, please read the switch expression RFC.

The switch expression RFC has proven to be somewhat controversial. Generally, I got the feeling that a switch expression is desirable. However, there is a fundamental disagreement on what that actually entails. I no longer know how to proceed with the RFC to make it more likely to pass. This poll tries to clear up this unknown.

The question seems to be if this new construct should be a variation of the switch to make it usable as an expression (like the RFC currently suggests) or if it should be an alternative to the switch statement that fixes all the issues mentioned in the RFC and essentially replaces it.

Switch expression

This is the approach currently described in the RFC.

// The switch statement gains multiple conditions per case
switch ($x) {
    case 1, 2, 3:
        {{statement_list}}
        break;
}
 
// The switch expression is added as described in the RFC
// * It is used in addition to the switch statement
// * It has multiple conditions per case
// * There's no fallthrough
// * If no condition is met and there's no default case an UnhandledSwitchCaseError is thrown
// * Each case can only contain a single expression
// * Type coercion remains
$y = switch ($x) {
    1, 2, 3 => {{expression}},
};

Switch replacement

// The switch statement stays exactly the same
switch ($x) {
    case 1:
        {{statement_list}}
        break;
}
 
// A new match (or other keyword) expression is created
// * It is virtually always preferred over the switch statement
// * It has multiple conditions per case
// * There's no fallthrough
// * If no condition is met and there's no default case a UnhandledSwitchCaseError is thrown
// * Each case can contain a single expression, or a block with a statement list if the expression result is discarded
// * There's no type coercion
$y = match ($x) {
    1, 2, 3 => {{expression}},
};
match ($x) {
    1, 2, 3 => {{expression}},
    4 => {
        {{statement_list}}
    },
}

Poll

I prefer the approach
Real name switch expression switch replacement none, the switch is fine something else
ajf (ajf)    
brzuchal (brzuchal)    
duncan3dc (duncan3dc)    
kalle (kalle)    
salathe (salathe)    
Final result: 1 1 1 2
This poll has been closed.
rfc/poll_switch_expression.txt · Last modified: 2020/04/11 23:50 by ilijatovilo