rfc:short-match
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:short-match [2020/12/14 00:18] – crell | rfc:short-match [2020/12/17 21:00] – Improve examples crell | ||
---|---|---|---|
Line 15: | Line 15: | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC makes only one change. | + | This RFC would make only one change. |
That is, this PHP 8.0 syntax: | That is, this PHP 8.0 syntax: | ||
Line 29: | Line 29: | ||
</ | </ | ||
- | Can, with this RFC, be shortened to: | + | could, with this RFC, be shortened to: |
<code php> | <code php> | ||
Line 41: | Line 41: | ||
</ | </ | ||
+ | A prime use case for such match statements is for basic pattern matching within a function: | ||
+ | |||
+ | <code php> | ||
+ | function getNumberKind(int $num) { | ||
+ | return match { | ||
+ | $num > 0 => NumberKind:: | ||
+ | $num == 0 => NumberKind:: | ||
+ | $num < 0 => NumberKind:: | ||
+ | }; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | That would be even more compact if combined with the also-proposed [[rfc: | ||
+ | |||
+ | <code php> | ||
+ | $getNumber = fn(int $num) => match { | ||
+ | $num < 0 => NumberKind:: | ||
+ | $num == 0 => NumberKind:: | ||
+ | $num > 0 => NumberKind:: | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | The net result is to simplify both the writing and reading of " | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 53: | Line 76: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | This is a simple up-or-down vote, requiring | + | Accept this RFC as proposed. |
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
Line 66: | Line 89: | ||
* [[rfc: | * [[rfc: | ||
* [[rfc: | * [[rfc: | ||
+ | * [[rfc: |
rfc/short-match.txt · Last modified: 2022/04/17 19:06 by ilutov