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
rfc:short-match [2020/12/14 00:40] crellrfc: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: Draft+  * Status: Inactive
   * First Published at: http://wiki.php.net/rfc/short-match   * First Published at: http://wiki.php.net/rfc/short-match
  
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 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.1607906406.txt.gz · Last modified: 2020/12/14 00:40 by crell