rfc:short-match

This is an old revision of the document!


PHP RFC: Short match

Introduction

The match() expression, introduced in PHP 8.0, matches one value against a series of others. For more complex cases, it's possible to provide a value of boolean true as the value to match against, allowing each branch to be arbitrarily complex as long as it results in a boolean. This RFC allows that case to be used as a default; It makes the value to match against optional, and defaults it to boolean true if not specified.

This functionality was included in the original Match expression v1 RFC. It was removed from the second Match expression v2 RFC for simplicity. However, the poll from the first RFC suggested this was a desired feature, so this RFC brings it back.

Proposal

This RFC makes only one change. It makes this syntax legal:

$a = 3;
 
print match {
  $a < 2 => 'small',
  $a == 3 => 'medium',
  default => 'large',
};

And it means exactly the same as this PHP 8.0 code:

$a = 3;
 
print match (true) {
  $a < 2 => 'small',
  $a == 3 => 'medium',
  default => 'large',
};

That is, if no match subject is provided, the match subject defaults to boolean true.

Backward Incompatible Changes

None

Proposed PHP Version(s)

8.1

Proposed Voting Choices

This is a simple up-or-down vote, requiring 2/3 to pass.

Patches and Tests

Implementation

References

rfc/short-match.1607904475.txt.gz · Last modified: 2020/12/14 00:07 by crell