rfc:match_expression
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:match_expression [2020/04/24 19:46] – Key is called return, not enter ilijatovilo | rfc:match_expression [2020/04/25 09:55] – Describe gotcha in (true) ilijatovilo | ||
---|---|---|---|
Line 93: | Line 93: | ||
<code php> | <code php> | ||
switch ($pressedKey) { | switch ($pressedKey) { | ||
- | case Key::RETURN: | + | case Key::RETURN_: |
save(); | save(); | ||
// Oops, forgot the break | // Oops, forgot the break | ||
Line 212: | Line 212: | ||
</ | </ | ||
- | For the time being the following code will result | + | For the time being using blocks in match expressions that use the return value in any way results |
<code php> | <code php> | ||
Line 218: | Line 218: | ||
0 => {}, | 0 => {}, | ||
}; | }; | ||
+ | //> Match that is not used as a statement can't contain blocks | ||
- | //> Match that not used as a statement can't contain blocks | + | foo(match ($x) { |
+ | 0 => {}, | ||
+ | }); | ||
+ | //> Match that is not used as a statement can't contain blocks | ||
+ | |||
+ | 1 + match ($x) { | ||
+ | 0 => {}, | ||
+ | }; | ||
+ | //> Match that is not used as a statement can't contain blocks | ||
+ | |||
+ | //etc. | ||
+ | |||
+ | // Only allowed form | ||
+ | match ($x) { | ||
+ | 0 => {}, | ||
+ | } | ||
</ | </ | ||
Line 245: | Line 261: | ||
</ | </ | ||
- | This introduces an ambiguity with the '' | + | This introduces an ambiguity with the '' |
<code php> | <code php> | ||
Line 261: | Line 277: | ||
</ | </ | ||
- | When '' | + | A '' |
<code php> | <code php> | ||
Line 267: | Line 283: | ||
$x = match ($y) { ... } - 1; | $x = match ($y) { ... } - 1; | ||
foo(match ($y) { ... } - 1); | foo(match ($y) { ... } - 1); | ||
- | $x[] = fn() => match ($y) { ... }; | + | $x[] = fn($y) => match ($y) { ... }; |
// etc. | // etc. | ||
+ | </ | ||
+ | |||
+ | This is also how Rust solves this ambiguity((https:// | ||
+ | |||
+ | <code rust> | ||
+ | match true { _ => () } - 1; | ||
+ | |||
+ | // warning: unused unary operation that must be used | ||
+ | // --> src/ | ||
+ | // | | ||
+ | // 2 | match true { _ => () } - 1; | ||
+ | // | ||
+ | // | | ||
</ | </ | ||
Because there was some controversy around this feature it was moved to a secondary vote. | Because there was some controversy around this feature it was moved to a secondary vote. | ||
+ | |||
+ | ===== Allow dropping (true) condition ===== | ||
+ | It has been suggested to make no condition equivalent to '' | ||
+ | |||
+ | <code php> | ||
+ | match { | ||
+ | $age >= 30 => {}, | ||
+ | $age >= 20 => {}, | ||
+ | $age >= 10 => {}, | ||
+ | default => {}, | ||
+ | } | ||
+ | |||
+ | // Equivalent to | ||
+ | |||
+ | match (true) { | ||
+ | $age >= 30 => {}, | ||
+ | $age >= 20 => {}, | ||
+ | $age >= 10 => {}, | ||
+ | default => {}, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The keyword '' | ||
+ | |||
+ | <code php> | ||
+ | match { | ||
+ | preg_match(...) => {}, // preg_match returns 1 which is NOT identical (===) to true | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Because I have no strong opinion on this it will be moved to a secondary vote. | ||
===== Miscellaneous ===== | ===== Miscellaneous ===== | ||
Line 279: | Line 339: | ||
<code php> | <code php> | ||
match ($x) { | match ($x) { | ||
- | | + | |
- | $this->methodCall() => ..., // methodCall | + | $this->bar() => ..., // bar() isn't called if foo() matched with $x |
- | $this->property | + | $this->baz => ..., |
// etc. | // etc. | ||
} | } | ||
Line 394: | Line 454: | ||
&& $value-> | && $value-> | ||
is_string($str) && $str !== '' | is_string($str) && $str !== '' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Explicit fallthrough ==== | ||
+ | Some people have suggested allowing explicit fallthrough to the next arm. This is, however, not a part of this RFC. | ||
+ | |||
+ | <code php> | ||
+ | match ($x) { | ||
+ | 1 => { | ||
+ | foo(); | ||
+ | fallthrough; | ||
+ | }, | ||
+ | 2 => { | ||
+ | bar(); | ||
+ | }, | ||
+ | } | ||
+ | |||
+ | // 1 calls foo() and bar() | ||
+ | // 2 only calls bar() | ||
+ | </ | ||
+ | |||
+ | This would require a few sanity checks with pattern matching. | ||
+ | |||
+ | <code php> | ||
+ | match ($x) { | ||
+ | $a => { fallthrough; | ||
+ | $b => { /* $b is undefined */ }, | ||
} | } | ||
</ | </ | ||
Line 440: | Line 527: | ||
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. | ||
- | |||
- | ===== Proposed PHP Version(s) ===== | ||
- | The proposed version is PHP 8. | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | As this is a language change, a 2/3 majority is required. The vote is a straight Yes/No vote for accepting the RFC and merging the patch. | + | As this is a language change, a 2/3 majority is required. |
<doodle title=" | <doodle title=" | ||
Line 459: | Line 543: | ||
</ | </ | ||
- | \\ | + | Secondary vote (choice with the most votes is picked): |
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ==== If you voted no, why? ==== | ||
+ | |||
+ | - Not interested | ||
+ | - I don't want blocks | ||
+ | - Missing return values in blocks | ||
+ | - Missing pattern matching | ||
+ | - Missing explicit fallthrough | ||
+ | - BC break is not acceptable | ||
+ | - Wanted [[https:// | ||
+ | - Other | ||
<doodle title=" | <doodle title=" | ||
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
+ | * 7 | ||
+ | * 8 | ||
</ | </ | ||
rfc/match_expression.txt · Last modified: 2020/05/09 15:59 by ilijatovilo