Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120076 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 79688 invoked from network); 20 Apr 2023 07:11:40 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 20 Apr 2023 07:11:40 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 439501804AA for ; Thu, 20 Apr 2023 00:11:40 -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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-oo1-f46.google.com (mail-oo1-f46.google.com [209.85.161.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 20 Apr 2023 00:11:39 -0700 (PDT) Received: by mail-oo1-f46.google.com with SMTP id h30-20020a4ae8de000000b005463ee651b8so413729ooe.3 for ; Thu, 20 Apr 2023 00:11:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681974699; x=1684566699; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=1r9kc/V2zlVukujAtdHarigm6Ud8X3qrC9AKycLs/vs=; b=iAfy+w91hnZbGm+1y7huASJkaEfTl9sLVwf/kx446L9p9vVXb/nuOjY1Ld1VmZXflv n9olageweMjOqbyF36jJx5Vf8inCFQnLgB/JBmWKuAl1M925Q986jbT9ZfqHxhQm7ioI L3qQkKYlcGEzqMJA54pOxtYHwyEQcYDTbiDnepmv90xgko2WKEgDFxMZIxs7Y7jKKGYJ wF6ER2TRdkKZ/F6OGUv99Ia1/Oen4Ox+/rGeDRClIeyTbO3xhE/AFr2fCe9m9bhkU4qA aUxsDbFDCF6qb4QtvTP7YeDwhPa/FPPxajN/TOnbUelOag9OsWTiOiosRCfsKAPkGEqa 977w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681974699; x=1684566699; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1r9kc/V2zlVukujAtdHarigm6Ud8X3qrC9AKycLs/vs=; b=Lrn9Mmz5oDdwLmza7JD3onyW7ZWjJXZ2tKN4eKOvuINb4nK5P+/6oSJ+P+CWZka4F3 7kSXpEtybbiz4dG8Ov+StZffaotXlUvBD0m1ulQC9VC97nggeurlKpK416t1oys+vIzp 83YSfSMKS67aHBrdQkiRdSyCE4NG5uV3JYXmmpUC3XtSSj6D71cHY3S5CC6N78DXI63u bx28J4Xa2WWwEsOwcL72tSM9z/U2uoL+XLVMMo5rQebncxV4CDdC4z1YKuEyviFsQxdU HvQND3KTuo9e5W27p8bDCOyr97zaGal+FI5PaGvNNTjxkx/vYAvh5Kh7TYHym+Lu+k/g 5m/Q== X-Gm-Message-State: AAQBX9dPytBE9OUFK+Mp7ddb4tqGe2ueSbFRwdLbXZahE9C6XTs6upkg AmN7ZVee61LTOdK8rNAOC2+ovBB7UOrFxFWOHFCI+l8YLdAecQ== X-Google-Smtp-Source: AKy350Zxw0Oe39GkjKCJr1aNxb0Yc+/Y1qyVOXdFD0OI6vEMZrN1tlROlbZLUcZi5EfmQrQbKmqiOZu2aOlrGEmv0lA= X-Received: by 2002:a4a:d0c2:0:b0:542:2321:658b with SMTP id u2-20020a4ad0c2000000b005422321658bmr1497015oor.2.1681974699127; Thu, 20 Apr 2023 00:11:39 -0700 (PDT) MIME-Version: 1.0 References: <687944e3-75ec-446b-bbd6-6d3d6856e864@app.fastmail.com> <1b842b25-c038-f647-67e1-b8c986b7d51d@bastelstu.be> <14DCA246-277A-44EC-B3A5-3809C5BCC7AA@gmail.com> In-Reply-To: <14DCA246-277A-44EC-B3A5-3809C5BCC7AA@gmail.com> Date: Thu, 20 Apr 2023 09:11:27 +0200 Message-ID: To: Rowan Tommins Cc: =?UTF-8?Q?Tim_D=C3=BCsterhus?= , Larry Garfield , php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] [Discussion] Clone with From: landers.robert@gmail.com (Robert Landers) On Thu, Apr 20, 2023 at 9:01=E2=80=AFAM Rowan Tommins wrote: > > On 18 April 2023 17:48:00 BST, "Tim D=C3=BCsterhus" wr= ote: > >I'd rather see only the fat-arrow being allowed. Unless I'm missing some= thing, braces with colon is not used anywhere else, whereas braces + '=3D>'= is known from match() and '=3D>' more generally is already used with array= literals [1]. Having two similar syntaxes for the same thing is not great = when both are commonly needed is not great. They need to be documented and = learned by developers. > > > I think it makes sense to have an unquoted form here, because the common = case is that they are names which analysers can match statically to particu= lar properties, not strings which will be analysed at runtime. There are pl= enty of places in the language where dynamic names are allowed, but we don'= t just use strings for the static case: > > ${'foo'} =3D 'bar'( constant('BAZ') )->{'quux'}(); > > > More specifically, the "name: $value" syntax matches named parameters, an= d while you can use an array for that (via ... unpacking), we don't force u= sers to do so. > > In fact, the Future Scope of that RFC considered the opposite: using the = colon syntax in arrays, along with a shorthand for pulling the key name fro= m the variable name: https://wiki.php.net/rfc/named_params#shorthand_syntax= _for_matching_parameter_and_variable_name > > > If I wanted to put these ideas into a general framework, I think one w= ay to go about this would be as follows: > > > > - Consider identifier: $expr as a shorthand for "identifier" =3D> $expr= . > > - Consider :$variable as a shorthand for variable: $variable and thus = "variable" =3D> $variable. > > > That would give us: > > $point =3D ['x' =3D> $x, 'y' =3D> $y, 'z' =3D> $z]; > $point =3D [x: $x, y: $y, z: $z]; > $point =3D [:$x, :$y, :$z]; > > $point =3D new Point(...['x' =3D> $x, 'y' =3D> $y, 'z' =3D> $z]); > $point =3D new Point(x: $x, y: $y, z: $z); > $point =3D new Point(:$x, :$y, :$z); > > $point =3D clone $point with {'x' =3D> $x, 'y' =3D> $y, 'z' =3D> $z}; > $point =3D clone $point with {x: $x, y: $y, z: $z}; > $point =3D clone $point with {:$x, :$y, :$z}; > > > > Rather than making everything use an array or array-like syntax, I would = probably go the other way and scrap the special syntax for dynamic names, m= aking the whole thing look like a function call, with support for array unp= acking: > > $point =3D clone $point with (x: $x, y: $y, z: $z); > $point =3D clone $point with (...['x' =3D> $x, 'y' =3D> $y, 'z' =3D> $z])= ; > > > > Regards, > > -- > Rowan Tommins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > I think using arrays here makes a lot of sense. For example, this example: public function withStatus($code, $reasonPhrase =3D ''): Response { return clone $this { $this->statusCode =3D $code; $this->reasonPhrase =3D "Old: $this->reasonPhrase, New: $reasonPhr= ase"; } }; could be rewritten with public function withStatus($statusCode, $reasonPhrase =3D ''): Response { // perform validation here $reasonPhrase =3D "Old: $this->reasonPhrase, New: $reasonPhrase"; return clone $this with compact('statusCode', 'reasonPhrase'); } I, personally, would find this much more ergonomic than writing out blocks of code and having a totally different syntax.