rfc:short-match
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:short-match [2020/12/14 00:07] – created crell | rfc:short-match [2022/04/17 19:06] (current) – Move to inactive ilutov | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Version: 1.0 | * Version: 1.0 | ||
* Date: 2020-12-13 | * Date: 2020-12-13 | ||
- | * Author: Larry Garfield (larry@garfieldtech.com | + | * Author: Larry Garfield (larry@garfieldtech.com) |
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
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: | ||
<code php> | <code php> | ||
$a = 3; | $a = 3; | ||
- | print match { | + | print match (true) |
$a < 2 => ' | $a < 2 => ' | ||
$a == 3 => ' | $a == 3 => ' | ||
Line 27: | Line 29: | ||
</ | </ | ||
- | And it means exactly the same as this PHP 8.0 code: | + | could, with this RFC, be shortened to: |
<code php> | <code php> | ||
$a = 3; | $a = 3; | ||
- | print match (true) | + | print match { |
$a < 2 => ' | $a < 2 => ' | ||
$a == 3 => ' | $a == 3 => ' | ||
Line 39: | Line 41: | ||
</ | </ | ||
- | That is, if no match subject | + | A prime use case for such match statements |
+ | |||
+ | <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 52: | 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 63: | Line 87: | ||
===== References ===== | ===== References ===== | ||
- | * [[rfc: | + | |
- | * [[rfc: | + | * [[rfc: |
+ | * [[rfc: |
rfc/short-match.1607904475.txt.gz · Last modified: 2020/12/14 00:07 by crell