Re: [RFC] Short-match
| From: | Larry Garfield | Date: | Thu, 17 Dec 2020 21:01:19 +0000 |
| Subject: | Re: [RFC] Short-match | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112543@lists.php.net to get a copy of this message | ||
On Thu, Dec 17, 2020, at 10:23 AM, Sara Golemon wrote:
> On Wed, Dec 16, 2020 at 6:50 PM someniatko <someniatko@gmail.com> wrote:
> >
> >
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):
> >
> > ```
> > $getNumber = fn(int $number) => match {
> > $number < 0 => NumberKind::NEGATIVE,
> > $number == 0 => NumberKind::ZERO,
> > $number > 0 => NumberKind::POSITIVE,
> > };
> > ```
> >
>
> That does read attractively, yes. This is the example that should have
> been offered first as it shows the expression nature shining.
>
> To contrast that with what would be possible now in an expressive
> functional form:
>
> $getNumber = fn(int $number) => [
> -1 => NumberKind::NEGATIVE,
> 0 => NumberKind::ZERO,
> 1 => NumberKind::POSITIVE,
> ][$number <=> 0];
>
> The match form *certainly* reads more naturally than the spaceship indexing
> form even though both take up equal space.
>
> -Sara
It looks like the quoted part from someniatko changed from a named function to an anon function?
Not sure what happened there.
I've included both a named and lambda version of his example in the RFC, however, and linked to
the short-functions RFC as that would allow the form he originally listed. They complement each
other nicely.
--Larry Garfield