On Wed, Mar 24, 2021 at 9:20 AM Larry Garfield <la...@garfieldtech.com>
wrote:

> 1) The updated short-functions RFC:
> https://wiki.php.net/rfc/short-functions
>
> 2) A new RFC, code by Nuno, for auto-capturing multi-statement closures:
> https://wiki.php.net/rfc/auto-capture-closure
>
> These are separate RFCs and at this time we plan to send them to separate
> votes, but I list them together to demonstrate that they have been
> developed in a mutually-compatible way.
>

Hi Larry (and Nuno) ,

Well done on the RFCs and making sure they are compatible and consistent.

That said, I agree with the sentiment that multi-statement auto-capturing
closures can add noise.  It's not just a multi-line `fn()`, as their usage
would be different.  With the single statement and return of `fn()` it was
intended to be used as a function argument where a callback is needed.
There autocapture makes sense to me.

But in the end, it's a matter of preference to use it or not.  I just don't
want PHP to drift closer to the callback hell that is JS.

Looking at the two proposals, I boil it down to this combined example:

```
class Short
{
    public function foo($a, $b, $c): int => match ($a) {
        1, 3, 5 => (fn() {
            $val = $a * $b;
            return $val * $c;
        })(),
        2, 4, 6 => (fn() {
            $val = $a + $b;
            return $val + $c;
        })(),
    };
}

class Long
{
    public function foo($a, $b, $c): int
    {
        return match ($a) {
            1, 3, 5 => self::bar($a, $b, $c),
            2, 4, 6 => self::baz($a, $b, $c),
        };
    }

    private static function bar($a, $b, $c): int
    {
        $val = $a * $b;
        return $val * $c;
    }

    private static function baz($a, $b, $c): int
    {
        $val = $a + $b;
        return $val + $c;
    }
}
```

The Short class is "fine", but I would still prefer the Long class.

Thanks,
Peter

Reply via email to