rfc:short-match

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:short-match [2020/12/14 00:07] – created crellrfc: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: Draft+  * Status: Inactive
   * First Published at: http://wiki.php.net/rfc/short-match   * First Published at: http://wiki.php.net/rfc/short-match
  
Line 15: Line 15:
 ===== Proposal ===== ===== Proposal =====
  
-This RFC makes only one change.  It makes this syntax legal:+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:
  
 <code php> <code php>
 $a = 3; $a = 3;
  
-print match {+print match (true) {
   $a < 2 => 'small',   $a < 2 => 'small',
   $a == 3 => 'medium',   $a == 3 => 'medium',
Line 27: Line 29:
 </code> </code>
  
-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 => 'small',   $a < 2 => 'small',
   $a == 3 => 'medium',   $a == 3 => 'medium',
Line 39: Line 41:
 </code> </code>
  
-That is, if no match subject is provided, the match subject defaults to boolean ''true''.+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 52: 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 63: Line 87:
 ===== References ===== ===== References =====
  
-* [[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.1607904475.txt.gz · Last modified: 2020/12/14 00:07 by crell