rfc:switch_expression

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:switch_expression [2020/03/28 00:59] – created ilijatovilorfc:switch_expression [2020/04/12 00:04] (current) – Link superseded by ilijatovilo
Line 1: Line 1:
 ====== PHP RFC: Switch expression ====== ====== PHP RFC: Switch expression ======
   * Date: 2020-03-28   * Date: 2020-03-28
-  * Author: Ilija Tovilo, ilija.tovilo@me.com +  * Author: Ilija Tovilo, tovilo.ilija@gmail.com 
-  * Status: Draft+  * Author: Michał Brzuchalski, brzuchal@php.net 
 +  * Status: Withdrawn
   * Target Version: PHP 8.0   * Target Version: PHP 8.0
   * Implementation: https://github.com/php/php-src/pull/5308   * Implementation: https://github.com/php/php-src/pull/5308
 +  * Previous RFC: https://wiki.php.net/rfc/switch-expression-and-statement-improvement
 +  * Superseded by RFC: https://wiki.php.net/rfc/match_expression
  
 ===== Introduction ===== ===== Introduction =====
Line 18: Line 21:
  
 <code php> <code php>
-$expressionResult = $condition switch {+$expressionResult = switch ($condition{
     1 => foo(),     1 => foo(),
     2 => bar(),     2 => bar(),
     3, 4, 5 => baz(),     3, 4, 5 => baz(),
-}+};
 </code> </code>
  
Line 66: Line 69:
  
 <code php> <code php>
-$y = $x switch {+$y = switch ($x{
     0 => 'Foo',     0 => 'Foo',
     1 => 'Bar',     1 => 'Bar',
Line 118: Line 121:
 </code> </code>
  
-The fallthrough behavior can't reasonably be changed in the ''switch'' statement because it would break a lot of code. However this RFC porposes allowing multiple conditions per ''case'' so that the intention of running the same code can be expressed more clearly. The ''switch'' expression resolves this issue exactly as described above. There is an implicit ''break'' added after each ''case''. Like with the statement multiple ''case'' conditions can be separated by a '',''.+The fallthrough behavior can't reasonably be changed in the ''switch'' statement because it would break a lot of code. However this RFC porposes allowing multiple conditions per ''case'' so that the intention of running the same code can be expressed more clearly. The ''switch'' expression resolves this issue exactly as described above. There is an implicit ''break'' added after each ''case''. Like with the statement multiple ''case'' conditions can be separated by a comma.
  
 ==== Inexhaustiveness ==== ==== Inexhaustiveness ====
Line 136: Line 139:
 </code> </code>
  
-The unexpected value will go unnoticed until the program crashes in a weird way, causes strange behavior or even worse becomes a security hole. Many languages can check if all the cases are handled at compile time or force you to write a ''default'' case if they can't. For a dynamic language like PHP the only alternative is throwing an exception. We can't reasonably change the exhaustiveness behavior in the ''switch'' statement because it would break a lot of code. The ''switch'' expression resolves this issue by throwing an ''InvalidArgumentException'' if the condition isn't met for any of the cases and the ''switch'' doesn't contain a ''default'' case.+The unexpected value will go unnoticed until the program crashes in a weird way, causes strange behavior or even worse becomes a security hole. Many languages can check if all the cases are handled at compile time or force you to write a ''default'' case if they can't. For a dynamic language like PHP the only alternative is throwing an error. We can't reasonably change the exhaustiveness behavior in the ''switch'' statement because it would break a lot of code. The ''switch'' expression resolves this issue by throwing an ''UnhandledSwitchCaseError'' if the condition isn't met for any of the cases and the ''switch'' doesn't contain a ''default'' case.
  
 <code php> <code php>
-$x switch {+switch ($x{
     1 => ...,     1 => ...,
     2 => ...,     2 => ...,
Line 164: Line 167:
  
 ===== Expression syntax ===== ===== Expression syntax =====
-The syntax is closely designed after [[https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#switch-expressions|C#'s switch expression]]. +There is an ambiguity problem with the empty ''switch'' statement vs expression:
- +
-Some people have asked why we don't reuse the syntax of the ''switch'' statement.+
  
 <code php> <code php>
-$x = switch ($y{} +// Could be a switch expression or a switch statement with an empty statement (;
-// instead of +switch ($x{};
-$x = $y switch {}+
 </code> </code>
  
-While this would be the preferred choice it is ambiguous.+To resolve it ambiguity empty switch expressions are not disallowed.
  
 <code php> <code php>
-switch ($x) {} +// This code throws a parser error 
-[$a] ...; +$x = switch ($y) {};
- +
-// Could also be interpreted as +
-switch ($y) {}[$a] = ...;+
 </code> </code>
- 
-Allowing this syntax would require making the parser context-sensitive which is undesirable. Another option would be to use a different keyword (e.g. ''match'') which would be a large backward incompatible change. 
  
 ===== "Why don't you just use x" ===== ===== "Why don't you just use x" =====
-The have been some comments on how you can already achieve the same result.+There have been some comments on how you can already achieve the same result.
  
 ==== if statements ==== ==== if statements ====
Line 216: Line 211:
 $y = $x === 1 ? ... $y = $x === 1 ? ...
   : ($x === 2 ? ...   : ($x === 2 ? ...
-  : (($x === 3 ? ...+  : ($x === 3 ? ...
   : 0));   : 0));
 </code> </code>
Line 226: Line 221:
  
 <code php> <code php>
-echo $x switch {+echo switch ($x{
     1 => {     1 => {
         foo();         foo();
rfc/switch_expression.1585357146.txt.gz · Last modified: 2020/03/28 00:59 by ilijatovilo