Re: [RFC] Short-match

From: Date: Tue, 15 Dec 2020 19:08:49 +0000
Subject: Re: [RFC] Short-match
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-112517@lists.php.net to get a copy of this message
On Mon, Dec 14, 2020 at 5:30 PM Andreas Leathley <a.leathley@gmx.net> wrote:
> 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)),
>      };
> };
> ```
>

Or even better with existing syntax:

```php
$this->handler = match($var) {
  null, true, false => json_encode($var),
  default => \is_string($var) ? "\"$var\"" : \rtrim(\print_r($var, true)),
};

I appreciate that this is a specific counter-example to your example, but
you picked the bad example. :p

-Sara


Thread (29 messages)

« previous php.internals (#112517) next »