On Sun, Apr 25, 2021 at 9:26 PM Larry Garfield <la...@garfieldtech.com>
wrote:

> Greetings, Internalians!
>
> I would like to offer for your consideration another RFC, specifically
> syntax for partial function application.
>
> https://wiki.php.net/rfc/partial_function_application
>
> It includes an implementation by Joe Watkins that is already about 95%
> complete.  (There's some edge cases he's still sorting out, but all of the
> typical cases should work already.)  Most of the design work comes from
> Levi Morrison and Paul Crovella.  I helped out with the tests, a few edge
> bits, and general instigator/nudge. :-)
>
> Discuss.
>
> --
>   Larry Garfield
>   la...@garfieldtech.com
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>
Heya, this is an interesting feature!

The way I understand it, it adds the ability to turn a call to an object
method into a callable?
```php
class CategorizedThings {
    private array $theThings = [];
    public function addOneThing(mixed $thing, string $category): void {
        $this->theThings[$category] = $thing;
    }
}

$things = new CategorizedThings();
$adder = $things->addOneThing(?, 'a category');

foreach ($repository->findAll() as $aThing) {
    $adder($aThing);
}

// which would be the same as:

$things = new CategorizedThings();
$adder = function (mixed $aThing) use ($things) {
    $things->addOneThing($aThing, 'a category');
}

foreach ($repository->findAll() as $aThing) {
    $adder($aThing);
}
```

I assume the following would also be possible?
```php
class Something {
    public static function toString(mixed $v): string {
        return (string) $v;
    }
}

function toString(mixed $v) : string {
    return (string) $v;
}

array_map(Something::toString(?), [1, 2, 3]);
array_map(toString(?), [1, 2, 3]);
// instead of
array_map([Something::class, 'toString'], [1, 2, 3])
array_map('toString', [1, 2, 3]);
```

If this is indeed the scenario, would it be worth adding a syntax to
indicate _all_ parameters can be placeholders? When a signature has
multiple arguments, `toString(?, ?, ?, ?)` could become `toString(...?)` or
maybe something like `toString(*)`?

Reply via email to