Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110915 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 86229 invoked from network); 10 Jul 2020 14:05:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Jul 2020 14:05:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 55FBA18053F for ; Fri, 10 Jul 2020 05:57:08 -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-il1-f175.google.com (mail-il1-f175.google.com [209.85.166.175]) (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 ; Fri, 10 Jul 2020 05:57:07 -0700 (PDT) Received: by mail-il1-f175.google.com with SMTP id k6so4960931ili.6 for ; Fri, 10 Jul 2020 05:57:07 -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; bh=UtnKTvkbDuj1PrmoeDJzbmbDY8GztN0ZNKbHdzOxtEI=; b=EnppfGEx7j0UYxD+eE8VEciSkl3O0SD9eptUyVe1A3VzFFy2ZdRAP/RaG5d8CcJ3h/ Pl5PrfGtHnCFMlSoPIAljcJiDx4hMXktF8SWJ2XKSVXDNP4zfNB6oRMeMuAUqaGDv2xe EszhfWS1XX+ide52dzin4gSXMEoif/BD2jDkllemuhjbSxZyqh6ROAQUV32pEWyqK6G2 OBs/cJvTH/WzICfkbzZIfmrX2svemNdBc7aIrMCaC5vgRTyBW41IThvqjF1j9tCt2c/x qY17SN8rP43PdX2bGWPvuXdgFFG5Qu0sKSaq8skVzKkZufcS79pJDMpH7nxR51AhmE7u KLqQ== 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; bh=UtnKTvkbDuj1PrmoeDJzbmbDY8GztN0ZNKbHdzOxtEI=; b=ZAShMqOz0x+Otq5e2/Hrsb8GG70gcPyL3lmgFUjOBMmvzPuG6uDs1kKuxLXTm6MU98 jB9brp8sfW0UEV354r9F9M3xpLRxZe+vPZlI2RhgGuMAtgr6JpBmSLFGX+H4d16X1pnq EwSEkYb6xun7bdqku87pjsq6TbovSWIrXyjvzeyfzoSBua9YKerN5RLHSYPjMrXyWWRi oFwUuqdk58O7JacVEJUa/BjcQQiaxay+H26uvru8ytcFjC/Z8TrpIM19yYhBny/S4RMm tgdFr7P/HtiSw8dtqggJo5d0Y5fdUeGFaY01DMegKiTaPUlYqu1VWjvNM5jrQebXSJsu h0mQ== X-Gm-Message-State: AOAM531a14pg6MgXOpRmBbvGsHIBmwVNIPbRWdqQS61w4i3ti8+225vY Oc0v6ROmgRpgkZw6DrfChtW2FTTf1s0OzcorsKtfCIes X-Google-Smtp-Source: ABdhPJyGvExRsnevZL1ievuRC4eb4v/vsv4aas5wfCbFS1Ch4Fuchx8kbcETngd4Wdq3sbq8XPSIbeGGDZpo46ig1n4= X-Received: by 2002:a92:5f12:: with SMTP id t18mr52790146ilb.267.1594385827052; Fri, 10 Jul 2020 05:57:07 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 10 Jul 2020 13:56:55 +0100 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="000000000000db428905aa15e00e" Subject: Re: [PHP-DEV] [VOTE] Named arguments From: rowan.collins@gmail.com (Rowan Tommins) --000000000000db428905aa15e00e Content-Type: text/plain; charset="UTF-8" On Fri, 10 Jul 2020 at 11:24, Marco Pivetta wrote: > If "array" is all you have to pass on as input, `fromArray` is what you > can certainly use: that's useful when de-serializing from JSON input, DB, > serialized state, etc. > > It certainly isn't meant to pass in the array via hand-crafted map: that's > what other (additional) named ctors would be for. > As I said in my previous response, I think this comes down to an unfortunate example - if what you actually have is an array, then yes, fromArray makes sense. One of the use cases for named parameters happens to look superficially similar to that, because it can be worked around by first creating an array, and then passing it somewhere. But the input you actually have is not an array, it is a set of variables from multiple sources, or a set of hard-coded parameters of different types that configure the functionality. One aspect I'd like to highlight is that named parameters aren't just useful for large numbers of individual parameters, but large numbers of _valid combinations_. For instance, with two optional parameters, you can quite easily have named methods to omit one or the other: function fromFoo($foo) function fromBar($bar) function fromFooAndBar($foo, $bar) You could even skip the third method by having optional parameters on one or both of the other two. But this list would increase exponentially; with only three parameters, you would need: function fromFoo($foo) function fromBar($bar) function fromBaz($baz) function fromFooAndBar($foo, $bar) function fromFooAndBaz($foo, $baz) function fromBarAndBaz($bar, $baz) function fromFooAndBarAndBaz($foo, $bar, $baz) Right now, there is no elegant way to combine these into one function signature. Named parameters are one solution, because they let you specify defaults for all parameters: function fromFooAndOrBarAndOrBaz($foo=42, $bar=true, $baz=[]) Another solution is parameter skipping, which was declined largely because people preferred named params: https://wiki.php.net/rfc/skipparams The only other alternative I know of is the builder pattern ($builder->withFoo($foo)->withBar($bar)->withBaz($baz)->build()), which at least makes the boilerplate scale linearly rather than exponentially, but is still going to require dozens of lines of code to achieve what named parameters can do with one. Regards, -- Rowan Tommins [IMSoP] --000000000000db428905aa15e00e--