rfc:match_expression_v2
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:match_expression_v2 [2020/05/18 20:32] – Create RFC ilijatovilo | rfc:match_expression_v2 [2020/07/09 22:06] (current) – Move to implemented ilutov | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Match expression v2 ====== | ====== PHP RFC: Match expression v2 ====== | ||
- | * Date: 2020-05-16 | + | * Date: 2020-05-22 |
* Author: Ilija Tovilo, tovilo.ilija@gmail.com | * Author: Ilija Tovilo, tovilo.ilija@gmail.com | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.0 | * Target Version: PHP 8.0 | ||
* Implementation: | * Implementation: | ||
- | * Supersedes: https:// | + | * Supersedes: https:// |
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes adding a new '' | + | This RFC proposes adding a new '' |
+ | |||
+ | [[https:// | ||
<code php> | <code php> | ||
- | $expressionResult | + | // Before |
- | | + | switch ($this-> |
- | | + | case Lexer:: |
- | default => baz(), | + | $statement = $this-> |
+ | break; | ||
+ | |||
+ | case Lexer:: | ||
+ | $statement = $this-> | ||
+ | break; | ||
+ | |||
+ | case Lexer:: | ||
+ | $statement = $this-> | ||
+ | break; | ||
+ | |||
+ | default: | ||
+ | $this-> | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | // After | ||
+ | $statement | ||
+ | | ||
+ | | ||
+ | Lexer:: | ||
+ | default => $this-> | ||
}; | }; | ||
</ | </ | ||
- | |||
===== Differences to switch ===== | ===== Differences to switch ===== | ||
==== Return value ==== | ==== Return value ==== | ||
- | It is very common that the '' | + | It is very common that the '' |
<code php> | <code php> | ||
switch (1) { | switch (1) { | ||
case 0: | case 0: | ||
- | $y = ' | + | $result |
break; | break; | ||
case 1: | case 1: | ||
- | $y = ' | + | $result |
break; | break; | ||
case 2: | case 2: | ||
- | $y = ' | + | $result |
break; | break; | ||
} | } | ||
- | echo $y; | + | echo $result; |
//> Bar | //> Bar | ||
</ | </ | ||
- | + | It is easy to forget assigning '' | |
- | It is easy to forget assigning '' | + | |
<code php> | <code php> | ||
Line 53: | Line 74: | ||
//> Bar | //> Bar | ||
</ | </ | ||
- | |||
==== No type coercion ==== | ==== No type coercion ==== | ||
- | The '' | + | The '' |
<code php> | <code php> | ||
switch (' | switch (' | ||
case 0: | case 0: | ||
- | | + | |
+ | break; | ||
+ | case ' | ||
+ | $result = "This is what I expected\n"; | ||
break; | break; | ||
} | } | ||
+ | echo $result; | ||
+ | //> Oh no! | ||
</ | </ | ||
- | + | The '' | |
- | The '' | + | |
<code php> | <code php> | ||
echo match (' | echo match (' | ||
- | 0 => "Never reached\n", | + | 0 => "Oh no!\n", |
+ | ' | ||
}; | }; | ||
+ | //> This is what I expected | ||
</ | </ | ||
- | |||
==== No fallthrough ==== | ==== No fallthrough ==== | ||
- | The '' | + | The '' |
<code php> | <code php> | ||
Line 88: | Line 113: | ||
} | } | ||
</ | </ | ||
- | + | The '' | |
- | This was intended to be a feature so that multiple conditions can execute the same block of code. It is often hard to understand if the missing | + | |
<code php> | <code php> | ||
- | switch | + | match ($pressedKey) { |
- | | + | |
- | case 2: | + | |
- | // Same for 1 and 2 | + | }; |
- | break; | + | |
- | | + | |
- | // Only 3 | + | |
- | case 4: | + | |
- | // Same for 3 and 4 | + | |
- | break; | + | |
- | } | + | |
</ | </ | ||
- | + | Multiple conditions can be comma-separated to execute the same block of code. | |
- | The '' | + | |
<code php> | <code php> | ||
Line 113: | Line 129: | ||
}; | }; | ||
</ | </ | ||
- | |||
==== Exhaustiveness ==== | ==== Exhaustiveness ==== | ||
- | Another large source of bugs is not handling all the possible cases supplied to the '' | + | Another large source of bugs is not handling all the possible cases supplied to the '' |
<code php> | <code php> | ||
- | switch ($configuration) { | + | switch ($operator) { |
- | case Config::FOO: | + | case BinaryOperator::ADD: |
- | | + | |
- | break; | + | |
- | case Config:: | + | |
- | // ... | + | |
break; | break; | ||
} | } | ||
+ | |||
+ | // Forgot to handle BinaryOperator:: | ||
</ | </ | ||
- | + | This will go unnoticed until the program crashes in a weird way, causes strange behavior or even worse becomes a security hole. '' | |
- | This 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> | ||
- | $y = match ($x) { | + | $result |
- | | + | |
- | 2 => ' | + | |
}; | }; | ||
- | // Only reached if $x is 1 or 2 | + | // Throws when $operator |
- | // $y is ' | + | |
</ | </ | ||
- | |||
===== Miscellaneous ===== | ===== Miscellaneous ===== | ||
==== Arbitrary expressions ==== | ==== Arbitrary expressions ==== | ||
- | A match condition can be any arbitrary expression. Analogous to '' | + | A match condition can be any arbitrary expression. Analogous to '' |
<code php> | <code php> | ||
- | $y = match ($x) { | + | $result |
foo() => ..., | foo() => ..., | ||
$this-> | $this-> | ||
Line 155: | Line 165: | ||
}; | }; | ||
</ | </ | ||
- | |||
===== Future scope ===== | ===== Future scope ===== | ||
Line 169: | Line 178: | ||
<code php> | <code php> | ||
- | $x = match { ... }; | + | $result |
// Equivalent to | // Equivalent to | ||
- | $x = match (true) { ... }; | + | $result |
</ | </ | ||
- | |||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | '' | + | '' |
* namespaces | * namespaces | ||
Line 184: | Line 192: | ||
Note that it will continue to work in method names and class constants. | Note that it will continue to work in method names and class constants. | ||
+ | |||
+ | ===== Syntax comparison ===== | ||
+ | https:// | ||
===== Vote ===== | ===== Vote ===== | ||
- | Voting | + | Voting |
+ | |||
+ | As this is a language change, a 2/3 majority is required. | ||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ |
rfc/match_expression_v2.1589833928.txt.gz · Last modified: 2020/05/18 20:32 by ilijatovilo