Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115168 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60287 invoked from network); 28 Jun 2021 09:08:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Jun 2021 09:08:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AF9071804C3 for ; Mon, 28 Jun 2021 02:28:22 -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-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.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 ; Mon, 28 Jun 2021 02:28:22 -0700 (PDT) Received: by mail-lf1-f54.google.com with SMTP id j4so31396985lfc.8 for ; Mon, 28 Jun 2021 02:28:22 -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=RNFM9pjvIwMerBnScqd+MoTqFrnTcfGWerXHJdUfiTU=; b=XDye/9sbIqx5ZqNYUOJ3RDgZLTZZKml0UjOZgrwiWGGZhuaG5AGY8ygbxjnOFmbZej BWSIAzqTDPseK/ftB71vAh8W12Ood7in3fNCXvcdtvvhcpXqHO6FTFlRFhj8At1js88X 5yH8GRe7nolclkUQ5w35kKDsttXyiZGeb/keznPpq95wUE2GYDs5eAT/dAlosEMYtn/e oxr89+f2/r9pfdddU4Qy5YMJmrJVWixudEsnB2BbxPkhGR/WDdN9p4X0TE7IKuD+MDlB OXgktmqXMbtSfrKN4/UWpYqxjBaAInWPx+/WbuSIcp3Vt9Za9MagdTeacBIS1332zKKT 1Yjg== 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=RNFM9pjvIwMerBnScqd+MoTqFrnTcfGWerXHJdUfiTU=; b=Knth4VvgScPu9KucnqubMl2zje5snQmMVip+T4eA3uX/5YYzpKq30B9TBA1cSYOxDa FKRCH6pXgC5KRjv6T/ZrCz7gz6wdilszMYx2f6yZXWprxuv3Y5YBlU5A7SP0D7KPlHP1 Tq4zf4j+vhiDEorolvYupHVVWPdbFA5gb3+zRoTRLdfZqt2Fvn7XD5tzXatN+Mv21ByE oqUEFoBiH0GCjOqBXAcVIf3x7f9wX5omKbI5Vb7sq4LfZZbRsy7K1OumPmDNetG2evKE dEvHfa4uvAK+PnPc7AdzzAjFl8Zchet2MJR+o8XkovwoccNQ03+vksXlNciskxcebaHd jOqw== X-Gm-Message-State: AOAM531NXXn+8WE03f+bI1iQIVUOawMe0gBDY38CzRCsIjb3pmf2YKV5 CZ2t++smQBT+DIBefd4SvpryU25mlSD4gsw6HKc= X-Google-Smtp-Source: ABdhPJz+dken/5+EBZviOxrfZZiF0mTN63r6+DJyRQDU/z4A0QRKG2+ctD07RvqWSk49KRZKG9XMZR9OIG+plAiy7Ac= X-Received: by 2002:ac2:4250:: with SMTP id m16mr18345862lfl.485.1624872499551; Mon, 28 Jun 2021 02:28:19 -0700 (PDT) MIME-Version: 1.0 References: <1ae611cb-b077-3db0-3237-c7ffa1c6e745@ralphschindler.com> In-Reply-To: <1ae611cb-b077-3db0-3237-c7ffa1c6e745@ralphschindler.com> Date: Mon, 28 Jun 2021 11:28:03 +0200 Message-ID: To: Ralph Schindler Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000024206405c5d01cda" Subject: Re: [PHP-DEV] [Proposal] call_user_func_map(): flexible named parameter mapping for call_user_func* From: nikita.ppv@gmail.com (Nikita Popov) --00000000000024206405c5d01cda Content-Type: text/plain; charset="UTF-8" On Sun, Jun 27, 2021 at 4:39 PM Ralph Schindler wrote: > The short proposal: > > > Make a variation of this work (notice parameter order and 'b' is goes > unused): > > call_user_func_map( > fn ($c, $a) => var_dump($a, $c), > [ > 'a' => 'the a value', > 'b' => 'the b value', > 'c' => 'the c value' > ] > ); > > // string(11) "the a value" > // string(11) "the c value" > > > > > The long proposal: > > > Callables, Closures, Anon Funcs, etc - have enjoyed increasing > popularity year over year. We've even gotten a shorter syntax for them > `fn ()` and more RFC's are sure to be on their way. > > This proposes a method for a publisher/framework/producer (the caller of > the callback) to document all the parameters that are available as named > parameters, and subscribers/consumers (creator of the callback) could > subscribe to what they need by name. > > For example: > > // documented callback parameters, this is what is available: > $params = [ > 'a' => 'value for a', > 'b' => 'value for b', > 'c' => 'value for c' > ]; > > // a consumer only interested in $a and $c could write: > $f = fn ($c, $a) => var_dump($a, $c); > > The consumer would then register their callback with the > producer/framework/etc or pass as the parameter to whatever is consuming it > > $callbackSystem->register('some_event', $f) > > // or > > $doSomething->with($f); > > The framework could then call with success: > > call_user_func_map($usersCallable, $allAvailableNamedParameters); > > > Currently the only way to do this is through array_diffing parameters > found via Reflection, then passing them along to call_user_func_array() > as a named array. > > Attempting to use call_user_func_array() without specifying all provided > variables results in: > > Uncaught Error: Unknown named parameter X in ... > > Thoughts? > Just to make sure I got this right: call_user_func_map() is the same as call_user_func_array(), but a) only accepts named params and b) silently ignores unknown named params? I'm not really convinced by your use-case here. This seems like a rather odd way to design an API, which goes against commonplace assumptions. If you pass a closure somewhere, the general expectation is that the parameter names you use are irrelevant and only the order matters. This creates a very unusual API contract, and while I can't prevent you from doing that, I'm pretty sure this is not something we want to endorse with additional library support. What's wrong with good old fn($params) => $params['a'] + $params['b']? (The same question for named parameters in general is answered by https://wiki.php.net/rfc/named_params#what_are_the_benefits_of_named_arguments -- notably none of the arguments made there are relevant in this context, the only benefit I see is saving a few keystrokes.) Regards, Nikita --00000000000024206405c5d01cda--