Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111164 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 365 invoked from network); 24 Jul 2020 13:31:21 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Jul 2020 13:31:21 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8AABB1804F2 for ; Fri, 24 Jul 2020 05:26:17 -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-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.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, 24 Jul 2020 05:26:16 -0700 (PDT) Received: by mail-pg1-f175.google.com with SMTP id t6so5148163pgq.1 for ; Fri, 24 Jul 2020 05:26:16 -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=E3W23yPG0UPW1Iit5CRHkESnM4Ppu6QHik+/ApIJygQ=; b=vfyM549nODXsn3GeKzKlwu4HrgXWdomWjA+gQLB2Hsua5lqDdIHbXr3DUoc1DZKLwv qOoJBNWmM0bUtyf0E41dIhloUDOhL1mhr0vqi3RxPvPfaxysmkHkkVSLLes6XKak9CQv 3qbt0kEb3T52rlWjYhFkA0ApS4LKJDNNWgCpWEPglt2CY2qzo4t+mX5Y1PUabUlCyw41 WOajuXUU2AadYKJfbEl/kWrEt0TCgN3+fUGZBsRNnNVkolVTFKDQsfP7eIupM9EGonJF Sm1maZ0SHbPI/f4u7skk8B0uVyC9kpfIfisN5/q8BDd8YYmppaPRj48Vp4B7u1AT3DTq rOqw== 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=E3W23yPG0UPW1Iit5CRHkESnM4Ppu6QHik+/ApIJygQ=; b=GX3C58n/jJFX9CddnmgLAArw5HqVEAuZbPYUiDc2okGnFqgN10LO7aZ8ZVgbutYRUa CdiXtT6fXG60UgeQk7YoM1mb3P92PjJvAmXp75WpzyRx5HTbgYCIOEOddZnK6bstwBof uJSQGn7SPQkc57w6WQ7BmDnGWMtZZmnXs0meHBn83C8CY0IPkasb7qZFKSqpq20IDpyn XCmvdhvFZ+DzQx1gAxCAIQj0ol92M+WUxhhsNA7f3BJQ1yMiqmhHSNNg+J2wFDp84NAM uqdJWZG0ANjpPiK6cm9wadP7eo1zFCAe/fzj+bcgNWxRsmUeHFveB7yRDuswNpBIskHH cLIA== X-Gm-Message-State: AOAM5337U/YRdbMXe1pgWw/5rf3Cjd2MHITo0KRkgR43yh4k9U5XkWDq IteUZCTkmA2hdIcXtUT747w81l+gEFZ3lh0+QoE= X-Google-Smtp-Source: ABdhPJw4NQK4YhcKmBF0V91WH7cxp9F5pTH6mQI2EE8C4kLZ67KAZpj3LyHOl2jMlfmPdR3XUAdiopBzyuzBekrg8Sc= X-Received: by 2002:a62:6484:: with SMTP id y126mr9258888pfb.166.1595593572823; Fri, 24 Jul 2020 05:26:12 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 24 Jul 2020 13:26:01 +0100 Message-ID: To: Marcio Almada Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000001d37cb05ab2f1443" Subject: Re: [PHP-DEV] [RFC][Proposal] Renamed parameters From: t.carnage@gmail.com (Chris Riley) --0000000000001d37cb05ab2f1443 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, 24 Jul 2020 at 13:05, Marcio Almada wrote: > Hi > > > > > Hi all, > > > > The named parameters RFC has been accepted, despite significant > objections > > from maintainers of larger OSS projects due to the overhead it adds to > > maintaining backwards compatibility as it has now made method/function > > parameter names part of the API; a change to them would cause a BC brea= k > > for any library users who decide to use the new feature. > > > > It is likely that the way this will shake out is that some maintainers > will > > accept the additional overhead of including parameter names in their BC > > guidelines and others will not, this leaves users unsure if they can us= e > > the new feature without storing up issues in potentially minor/security > > releases of the libraries they use. This is not really an ideal > situation. > > > > More pressing a point is that the current implementation breaks object > > polymorphism. Consider this example (simplified from one of my codebase= s) > > > > interface Handler { > > public function handle($message); > > } > > > > class RegistrationHandler implements Handler { > > public function handle($registraionCommand); > > } > > > > class ForgottenPasswordHandler implements Handler { > > public function handle($forgottenPasswordCommand); > > } > > > > class MessageBus { > > //... > > public function addHandler(string $message, Handler $handler) { > //... } > > public function getHandler(string $messageType): Handler { //... } > > public function dispatch($message) > > { > > $this->getHandler(get_class($message))->handle(message: > $message); > > } > > } > > > > This code breaks at run time. > > > > Proposals were made for resolutions to this issue however all of them > > require trade offs and could potentially break existing code. > > > > My proposal to resolve these two issues is to add the ability to rename > > parameters with a new syntax as follows. > > > > function callBar(Foo $internalName:externalName) { > > $internalName->bar(); > > } > > > > $x =3D new Foo(); > > callBar(externalName: $x); > > > > This allows both the above problems to be resolved, by renaming the > > internal parameter and keeping the external signature the same. > > > > I propose that the RFC would have two voting options. > > > > The first would be to implement it as proposed above, this would allow > any > > parameter to be called by name regardless of the intentions of the auth= or > > of the method/function and is closest to the current behaviour. > > > > The second option would be to use this syntax to make named parameters = in > > userland code explicitly opt in. As such an additional shortcut syntax > > would be implemented: $: to designate a named parameter. eg > > > > function callBar($:externalName) { > > $externalName->bar(); > > } > > > > $x =3D new Foo(); > > callBar(externalName: $x); > > > > If a parameter is not opted in, a compile time error is raised: > > > > function callBar($externalName) { > > $externalName->bar(); > > } > > > > $x =3D new Foo(); > > callBar(externalName: $x); // Error: cannot call parameter $externalNam= e > by > > name. > > > > There are pros and cons to this second approach, on the one hand it > reduces > > the usefulness of the named parameter syntax by requiring changes to ol= d > > code to enable it (although this could probably be automated fairly > easily) > > however it does provide a neater solution to the second problem in that= , > to > > prevent the runtime errors in the second issue example, every child cla= ss > > would need to use the rename syntax on it's parameter to prevent errors= , > > whereas if we went down this route, the parent class could just not opt > > into the named parameter syntax and the code would function as expected= . > > > > Another advantage is that with the ability to rename parameters using t= he > > opt in, we gain some flexibility to tighten up the LSP rules relating t= o > > named parameter inheritance. > > > > class Foo { > > public function bar($:param) { //... } > > public function baz($internal:external) { //... } > > } > > > > // OK > > class Bar { > > public function bar($renamed:param) { //... } > > public function baz($renamed:external) { //... } > > } > > > > // Compile time error cannot rename named parameter $:param (renamed to > > $:renamedParam) > > class Baz { > > public function bar($:renamedParam) { //... } > > } > > > > // Compile time error cannot rename named parameter $:external (renamed > to > > $:renamed) > > class Baz { > > public function baz($internal:renamed) { //... } > > } > > > > While this would be technically possible with the first option (no opt > in) > > it would break any existing code which renames a parameter as every > > parameter would be subject to these rules. > > > > I don't have Wiki karma so can't post this yet; but I want to get the > ball > > rolling on discussion as feature freeze is coming up fast and if we wan= t > to > > go for the second option, that must hit before the named parameter synt= ax > > is in a tagged version of PHP. > > > > Regards, > > Chris > > For reference, I believe you are proposing something > close to Swift external names but way more complex. > > Ty, > M=C3=A1rcio > Hi, I've never written any code in swift, however having quickly read the documentation I think that my proposal is almost identical to swift argument labels if the first option is chosen. It's interesting to note that swift provides an explicit opt out of named parameters instead of an opt in (by giving it a _ for it's external name) this could be an alternative option for this RFC, although I'd propose the $: syntax in that instance. You'd lose the ability to do compile time checks on parameter renames in child classes; but it might provide a pragmatic middle ground between my two proposed options. Thanks, Chris --0000000000001d37cb05ab2f1443--