On Wed, May 12, 2021, at 4:51 AM, Nicolas Grekas wrote: > > > This makes me wonder: can we create a partial programmatically? Wouldn't > > > that be needed for some use cases? > > > Partial::createFromCallable($callable, the-args)? > > > > > > Nicolas > > > > I cannot think of a use case where that would be needed. Since you can > > partial-ize any callable, including a dynamic one, if you needed to do > > something like partial-ize one of a series of function calls you can do > > that already: > > > > $c = match($some_input) { > > 'A' => 'func_a', > > 'B' => 'func_b', > > 'C' => 'func_c', > > }; > > > > $p = $c(1, 2 ?, 4); > > > > Though at that point, just partialing them in the first place inside the > > match would be better as then you never have a function name in a string to > > begin with. > > > > Here is a use case: high-order argument resolvers / function reducers. > > What I mean is a function that takes a callable as arguments, resolves as > many args of the callable as it can using whatever logic fits, and returns > a callable with fewer arguments (only the non-resolved ones - aka a > Partial).
You're thinking something like an auto-wiring routine for callables? function volume(int $x, int $y, int $z) { ... } class Resolver { private array $context = ['x' => 5]; public resolve(callable $c) { foreach (get_argument_names_from_param($c) as $k) { if (isset($this->context[$k]) { $args[$k] = $this->context[$k]; } } return $c(...$args, ?); } } $r = new Resolver(); $c2 = $r->resolve(volume(?)); print $c2(y: 3, z: 9); I haven't tried running that (I don't feel like messing with the necessary reflection), but... I think it would work already? Whether that's useful in practice or not I don't know. :-) --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php