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
Next revisionBoth sides next revision
rfc:switch_expression [2020/03/28 10:52] – Minor rewording ilijatovilorfc:switch_expression [2020/03/28 21:37] – Replace InvalidArgumentException with UnhandledSwitchCaseError 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: Under Discussion
   * 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
Line 120: Line 120:
 </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 138: Line 138:
 </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>
Line 218: Line 218:
 $y = $x === 1 ? ... $y = $x === 1 ? ...
   : ($x === 2 ? ...   : ($x === 2 ? ...
-  : (($x === 3 ? ...+  : ($x === 3 ? ...
   : 0));   : 0));
 </code> </code>
rfc/switch_expression.txt · Last modified: 2020/04/12 00:04 by ilijatovilo