rfc:short-match
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:short-match [2020/12/14 00:40] – crell | rfc:short-match [2022/04/17 19:06] (current) – Move to inactive ilutov | ||
---|---|---|---|
Line 3: | Line 3: | ||
* 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 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 66: | Line 89: | ||
* [[rfc: | * [[rfc: | ||
* [[rfc: | * [[rfc: | ||
+ | * [[rfc: |
rfc/short-match.1607906406.txt.gz · Last modified: 2020/12/14 00:40 by crell