====== PHP RFC: Switch Expression ====== * Version: 1.0 * Date: 2019-10-18 * Author: MichaƂ Brzuchalski * Status: Draft * First Published at: https://wiki.php.net/rfc/switch-expression ===== Introduction ===== The current design of PHP's switch statement follows closely languages such as C/C++ and supports fall-through semantics by default. The traditional switch statement is unnecessarily verbose which often leads to missing break statements mean that accidentally fall-through occurs. The idea of this RFC is to introduce switch as an expression which can use simplified control flow behaviour and scope similar to arrow functions. ===== Proposal ===== The motivation behind this proposal is to provide a simplified control flow and direct return value allowed to be used as an expression. Following example shows how a switch statement can be unnecessarily verbose and how visual noise could lead to accidental fall-through occurs. ==== Syntax ==== The proposal is to introduce a new form of switch label, written "case C =>" to signify that only the code to the right of the label is to be executed if the label is matched and in consequence value of that code should be the return value of switch expression. Following example demonstrates noise and verbosity reduce. "weekend!"; case 1, 2, 3, 4, 5 => "weekday :("; case 6 => "weekend!"; }; echo "Today is {$say}"; > **Note!** Switch expression allows evaluating single expression just like the arrow function. This restriction can be relaxed in the future. ==== Non-completeness ==== The witch statement returning errors with ''RuntimeException'' if none of the labels evaluated on a match. "matched 'foo'!"; case "bar" => "matched 'bar'"; }; // This trows RuntimeException cause there is no default clause and whole expression is not complete ===== Backward Incompatible Changes ===== None. ===== Proposed PHP Version(s) ===== Targets next PHP 8.x. ===== RFC Impact ===== ==== To SAPIs ==== None. ==== To Existing Extensions ==== None. ==== To Opcache ==== Would require opcache changes. ===== Future Scope ===== ==== Return type ==== The switch expression errors with ''RuntimeException'' if return type given and there is a type mismatch. "weekend!"; case 1, 2, 3, 4, 5 => "weekday :("; case 6 => true; // This throws TypeError when resulting expression evaluates with different type }; echo "Today is {$say}"; ==== Splat operator ==== Grouping labels into a comma-separated list could benefit from splat operator replacing a bunch of labels. "matched 'foo' or 'bar', requires further manual verification"; }; ===== Proposed Voting Choices ===== As this is a language change, a 2/3 majority is required. The vote is a straight Yes/No vote for accepting the RFC and merging the patch. ===== Patches and Tests ===== Not implemented. ===== Implementation ===== ===== References ===== * [[http://openjdk.java.net/jeps/325|Java JEP 325: Switch Expressions]] * [[https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#switch-expressions|C# 8.0: Switch Expression]]