rfc:match_expression
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast 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 10:37] – Specify voting time range ilijatovilo | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2020-04-12 | * Date: 2020-04-12 | ||
* 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: | ||
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 ===== | ||
==== 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> | ||
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 377: | Line 437: | ||
// Algebraic data types if we ever get them | // Algebraic data types if we ever get them | ||
- | let Ast\BinaryExpr($lhs, | + | let Ast::BinaryExpr($lhs, |
} | } | ||
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. | + | Voting starts 2020-04-25 |
- | <doodle title=" | + | As this is a language change, a 2/3 majority is required. |
+ | |||
+ | <doodle title=" | ||
* Yes | * Yes | ||
* No | * No | ||
Line 454: | Line 540: | ||
Secondary vote (choice with the most votes is picked): | Secondary vote (choice with the most votes is picked): | ||
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No | ||
</ | </ | ||
- | \\ | + | 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