What about matching on a variable's type?
```
match {
$var: string => "is a string"
$var: array => "something else"
}
```
This could be used with flow-sensitive typing, e.g. assume the type of $var
being string in the string block. Psalm works like this for if-statements.
Also consider the case with generics.
Compare with generalised algebraic data types in FP (GADT).
Olle
On Thu, 17 Dec 2020, 22:01 Larry Garfield, <[email protected]> wrote:
> On Thu, Dec 17, 2020, at 10:23 AM, Sara Golemon wrote:
> > On Wed, Dec 16, 2020 at 6:50 PM someniatko <[email protected]> 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
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>