rfc:switch_expression
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:switch_expression [2020/03/28 12:29] – Move to Under Discussion ilijatovilo | rfc: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, | + | * Author: Ilija Tovilo, tovilo.ilija@gmail.com |
* Author: Michał Brzuchalski, | * Author: Michał Brzuchalski, | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.0 | * Target Version: PHP 8.0 | ||
* Implementation: | * Implementation: | ||
* Previous RFC: https:// | * Previous RFC: https:// | ||
+ | * Superseded by RFC: https:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
Line 20: | Line 21: | ||
<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 68: | Line 69: | ||
<code php> | <code php> | ||
- | $y = $x switch | + | $y = switch ($x) { |
0 => ' | 0 => ' | ||
1 => ' | 1 => ' | ||
Line 138: | Line 139: | ||
</ | </ | ||
- | 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 166: | Line 167: | ||
===== 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 expression or 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" ===== | ||
Line 218: | Line 211: | ||
$y = $x === 1 ? ... | $y = $x === 1 ? ... | ||
: ($x === 2 ? ... | : ($x === 2 ? ... | ||
- | : (($x === 3 ? ... | + | : ($x === 3 ? ... |
: 0)); | : 0)); | ||
</ | </ | ||
Line 228: | Line 221: | ||
<code php> | <code php> | ||
- | echo $x switch | + | echo switch ($x) { |
1 => { | 1 => { | ||
foo(); | foo(); |
rfc/switch_expression.1585398573.txt.gz · Last modified: 2020/03/28 12:29 by ilijatovilo