rfc:switch_expression

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:switch_expression [2020/03/28 11:31] – Minor rewording 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
   * Author: Michał Brzuchalski, brzuchal@php.net   * Author: Michał Brzuchalski, brzuchal@php.net
-  * Status: Draft+  * 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   * 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 20: 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 68: Line 69:
  
 <code php> <code php>
-$y = $x switch {+$y = switch ($x{
     0 => 'Foo',     0 => 'Foo',
     1 => 'Bar',     1 => 'Bar',
Line 138: 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 166: 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" =====
Line 218: Line 211:
 $y = $x === 1 ? ... $y = $x === 1 ? ...
   : ($x === 2 ? ...   : ($x === 2 ? ...
-  : (($x === 3 ? ...+  : ($x === 3 ? ...
   : 0));   : 0));
 </code> </code>
Line 228: Line 221:
  
 <code php> <code php>
-echo $x switch {+echo switch ($x{
     1 => {     1 => {
         foo();         foo();
rfc/switch_expression.1585395097.txt.gz · Last modified: 2020/03/28 11:31 by ilijatovilo