RE: [PHP-DEV] [RFC] Short-match
match is an expression, where as if-else construction is not. This
allows for combining it with a potential future feature of single line
functions and methods. For example (hypothetical syntax is used):
```
function getNumberKind(int $number) => match {
$number > 0 => NumberKind::POSITIVE,
$number == 0 => NumberKind::ZERO,
$number < 0 => NumberKind::NEGATIVE,
}
```
See how natural it looks and reads.
Another improvement is that it reduces the amount of visual clutter
(if and return keywords).
Yet another distinction between “if” and “match” is their intent. If
is more appropriate for the procedural flow. However, when we operate
with pure functions, match is a good sign of such function. It shows
that different input values turn into different output values, instead
of different branches of procedural code being executed. Therefore, if
code is intended to be more FP-ish, IMO, match is preferred. For the
procedural code patterns, including early returns / throws, if is
clearly a better choice.
So, match is better suited for functional-ish parts of code, which
do not pose behavior, but only convert one value to another ones. In
the FP world, pattern matching is a widely used feature, allowing to
split computations of output value for different kinds of input value.
The closest thing available in PHP these days is match (true). The
(true) part looks ugly and artificial, and removing it is a matter
of a cosmetic change.
Of course, it is possible to use match instead of switch or
ifs
for procedural code, and vice versa, which is still syntaxically
correct, but bad from an intent expression perspective. This is a
downside of match expression as a whole and, IMO, is irrelevant for
this small improvement.
So,
- match allows writing clearer code when used with FP-ish intent; it
is also an expression, therefore irreplaceable with if/switch in some
cases.
- pattern matching is a widely used feature in FP, and match (true)
is the current way to achieve the most similar thing in PHP.
- improving match will not hurt the language allowing to write the
same thing two ways – because it is already done by accepting match
RFC itself