rfc:short-match

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
rfc:short-match [2020/12/14 00:18] crellrfc:short-match [2020/12/17 21:00] – Improve examples crell
Line 15: Line 15:
 ===== Proposal ===== ===== Proposal =====
  
-This RFC makes only one change.  In a ''match()'' expression, if the match subject is not provided, it defaults to boolean ''true''.+This RFC would make only one change.  In a ''match()'' expression, if the match subject were not provided, it would default to boolean ''true''.
  
 That is, this PHP 8.0 syntax: That is, this PHP 8.0 syntax:
Line 29: Line 29:
 </code> </code>
  
-Can, with this RFC, be shortened to:+could, with this RFC, be shortened to:
  
 <code php> <code php>
Line 41: Line 41:
 </code> </code>
  
 +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::POSITIVE,
 +    $num == 0 => NumberKind::ZERO,
 +    $num < 0 => NumberKind::NEGATIVE,
 +  };
 +}
 +</code>
 +
 +That would be even more compact if combined with the also-proposed [[rfc:short-functions|Short Functions]] RFC.  Or, if used with a short-lambda today:
 +
 +<code php>
 +$getNumber = fn(int $num) => match {
 +    $num < 0 => NumberKind::NEGATIVE,
 +    $num == 0 => NumberKind::ZERO,
 +    $num > 0 => NumberKind::POSITIVE,
 +};
 +</code>
 +
 +The net result is to simplify both the writing and reading of "functions as expressions" rather than "functions as subroutines."
  
 ===== 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 2/3 to pass.+Accept this RFC as proposed. 2/3 required for passage.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 66: Line 89:
   * [[rfc:match_expression|PHP RFC: Match expression]]   * [[rfc:match_expression|PHP RFC: Match expression]]
   * [[rfc:match_expression_v2|PHP RFC: Match expression v2]]   * [[rfc:match_expression_v2|PHP RFC: Match expression v2]]
 +  * [[rfc:short-functions|PHP RFC: Short Functions]]
rfc/short-match.txt · Last modified: 2022/04/17 19:06 by ilutov