Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114454 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 67507 invoked from network); 12 May 2021 16:29:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 May 2021 16:29:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 10F401804B1 for ; Wed, 12 May 2021 09:37:09 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 12 May 2021 09:37:08 -0700 (PDT) Received: by mail-ed1-f54.google.com with SMTP id f1so5611676edt.4 for ; Wed, 12 May 2021 09:37:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=4cqVKppnFoRh9qeD3yx6M82JUzw/vT7xPCmaQZa7drw=; b=T2bwvvTEogVI0EMDUSguR/II8AFvaMXTDWcZOqWKeTwkpUmxdNmLSSra2fGgbvfLft CTtdw+oqNYUFhWNSFtZJ6mMA4fULbUAiQFOm7XZOE+RAesS0YPnSkqvREoarHtI4fiKw pFJnbPBMkNzhNKy+dO11KBStLP0FWHwbSll5eUMpQPeh0K+q8j/VnHW8UwYOmH6lBD3v w+7ZWkiHZ2JNr43bOxK5Be7cRQ1ifcGaAT3o0gkDky0e+uvuQepSDl8qzAz1h6sIDzyu lcCf1Jjl2w4JwzJXsQrxmE3IY8cH8EV/Ymdiwm8CbykuyO/6/FLoD2wE0tjX/DdOk2B0 Mf9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=4cqVKppnFoRh9qeD3yx6M82JUzw/vT7xPCmaQZa7drw=; b=ZPJNJL4Q5CyHr2q2MRRPIfIfcOITdFiaOBTl+MH7f1to4ULvGUOs5l9zuFFfv9dTdp rAo5tkWFoBwHp2km6VtTahJTEDGH6NiR+0Pu/tSj67TB7g4c99AokaFGUdUGlmAEkEae lws1cvaoLWQPAooszRSKbTfXb3yuwbK5BV2Nmn8112M5iSX9VH9ZGKrTno9Pm7W2BP/a JFRCilZpyk5T4e9nUp3ofWO8iRCt0bN3eKqDS6uFH/wqxEqNdriiFnfqRgshsi29EqdL dV1/eCb3JSl0x4t1z9sjZhu1VCyT/+AOeoOXBD1WBJ6VtJZwVsoRsXkqHMnNqVb98VKW 4ajg== X-Gm-Message-State: AOAM5302zt8ftTgMEOrObkDrX6IPIlIOCUY/6xzqrVdRvVD4XYa58I6Y ms520Tl+e0etYP2QTAt0BEBRLebLLhKM4DZA3hXV7p5e54gT3A== X-Google-Smtp-Source: ABdhPJw+1tAt+QhtmlyV40clkv+g4pbx/C6WNMW0tGN94C6PRgYNvTUB71wR4F0cVXNhbh2eTCtn4QxkCaSfPib3EIc= X-Received: by 2002:aa7:d917:: with SMTP id a23mr43509523edr.260.1620837422663; Wed, 12 May 2021 09:37:02 -0700 (PDT) MIME-Version: 1.0 References: <5650fab5-6006-4af2-b337-9e4aba67bf04@www.fastmail.com> <13142d57-f901-462c-a38f-b146ab36497a@www.fastmail.com> In-Reply-To: Date: Wed, 12 May 2021 18:36:50 +0200 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000d10b3805c2249edc" Subject: Re: [PHP-DEV] [RFC] Partial function application From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --000000000000d10b3805c2249edc Content-Type: text/plain; charset="UTF-8" > > > > > 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. :-) > Oh, indeed, that's super great! Then remains my question about replacing Partial by Closure. I think this would be an important feature and that would remove the need for ReflectionPartial. About reflection, the following behaviors are quite problematic to me: > ReflectionFunction(Closure::fromCallable($arbitrary_callable)) to normalize all callables to a single reflection interface will no longer work I don't understand why. Unless there is a rationale to that (which one?) this looks arbitrary. I very often see code like: if (!$callable instanceof Closure) $callable = Closure::fromCallable($callable); $r = ReflectionFunction($callable) The limitation of described in the RFC will make the equivalent boilerplate much more complex in order to support 8.1 I would like this to be improved or at least better explained. Cheers, Nicolas --000000000000d10b3805c2249edc--