Re: [RFC] Short-match

From: Date: Tue, 15 Dec 2020 09:10:54 +0000
Subject: Re: [RFC] Short-match
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-112514@lists.php.net to get a copy of this message
Hi internals

> With match this becomes much more concise:
>
> ```php
> $this->handler = function ($var): string {
>      return match {
>          null === $var => 'null',
>          true === $var => 'true',
>          false === $var => 'false',
>          \is_string($var) => '"'.$var.'"',
>          default => rtrim(print_r($var, true)),
>      };
> };
> ```

I suspect most use cases of match (true) are exactly for things like
this where you're always comparing the same value but not against
fixed values but "shapes". I think some simple and limited pattern
matching would be useful here.

```php
$this->handler = function ($var): string {
     return match ($var) {
         null => 'null',
         true => 'true',
         false => 'false',
         is string => '"'.$var.'"',
         default => rtrim(print_r($var, true)),
     };
};
```

This would have some significant overlap with the short match proposed
here. Combined with the unclear semantics (strict comparison? loose
comparison? type error on non-bool values?) makes me unsure if we
really need the short match.

Ilija


Thread (29 messages)

« previous php.internals (#112514) next »