rfc:switch_expression
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:switch_expression [2020/03/28 00:59] – created ilijatovilo | rfc:switch_expression [2020/04/01 08:31] – Switch to `switch ($x)` syntax ilijatovilo | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Switch expression ====== | ====== PHP RFC: Switch expression ====== | ||
* Date: 2020-03-28 | * Date: 2020-03-28 | ||
- | * Author: Ilija Tovilo, | + | * Author: Ilija Tovilo, tovilo.ilija@gmail.com |
- | * Status: | + | * Author: Michał Brzuchalski, |
+ | * Status: | ||
* Target Version: PHP 8.0 | * Target Version: PHP 8.0 | ||
* Implementation: | * Implementation: | ||
+ | * Previous RFC: https:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
Line 18: | Line 20: | ||
<code php> | <code php> | ||
- | $expressionResult = $condition | + | $expressionResult = switch ($condition) { |
1 => foo(), | 1 => foo(), | ||
2 => bar(), | 2 => bar(), | ||
3, 4, 5 => baz(), | 3, 4, 5 => baz(), | ||
- | } | + | }; |
</ | </ | ||
Line 66: | Line 68: | ||
<code php> | <code php> | ||
- | $y = $x switch | + | $y = switch ($x) { |
0 => ' | 0 => ' | ||
1 => ' | 1 => ' | ||
Line 118: | Line 120: | ||
</ | </ | ||
- | The fallthrough behavior can't reasonably be changed in the '' | + | The fallthrough behavior can't reasonably be changed in the '' |
==== Inexhaustiveness ==== | ==== Inexhaustiveness ==== | ||
Line 136: | Line 138: | ||
</ | </ | ||
- | 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 '' | + | 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 '' |
<code php> | <code php> | ||
- | $x switch | + | switch ($x) { |
1 => ..., | 1 => ..., | ||
2 => ..., | 2 => ..., | ||
Line 164: | Line 166: | ||
===== Expression syntax ===== | ===== Expression syntax ===== | ||
- | The syntax | + | There is an ambiguity problem with the empty '' |
- | + | ||
- | Some people have asked why we don't reuse the syntax of the '' | + | |
<code php> | <code php> | ||
- | $x = switch ($y) {} | + | // Could be a switch |
- | // instead of | + | switch ($x) {}; |
- | $x = $y switch | + | |
</ | </ | ||
- | While this would be the preferred choice | + | To resolve |
<code php> | <code php> | ||
- | switch ($x) {} | + | // This code throws a parser error |
- | [$a] = ...; | + | $x = switch ($y) {}; |
- | + | ||
- | // Could also be interpreted as | + | |
- | switch ($y) {}[$a] = ...; | + | |
</ | </ | ||
- | |||
- | Allowing this syntax would require making the parser context-sensitive which is undesirable. Another option would be to use a different keyword (e.g. '' | ||
===== "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 210: | ||
$y = $x === 1 ? ... | $y = $x === 1 ? ... | ||
: ($x === 2 ? ... | : ($x === 2 ? ... | ||
- | : (($x === 3 ? ... | + | : ($x === 3 ? ... |
: 0)); | : 0)); | ||
</ | </ | ||
Line 226: | Line 220: | ||
<code php> | <code php> | ||
- | echo $x switch | + | echo switch ($x) { |
1 => { | 1 => { | ||
foo(); | foo(); |
rfc/switch_expression.txt · Last modified: 2020/04/12 00:04 by ilijatovilo