Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109333 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2730 invoked from network); 26 Mar 2020 14:14:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Mar 2020 14:14:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 86E381804DD for ; Thu, 26 Mar 2020 05:38:57 -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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8560 212.227.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mout.web.de (mout.web.de [212.227.15.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 26 Mar 2020 05:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1585226333; bh=4KeiC2f3TK8GA7A44PQAV715/ocLFbwzDw9DA7V37nk=; h=X-UI-Sender-Class:Subject:To:Cc:References:From:Date:In-Reply-To; b=Bbb+0cANuh19ZXf5xk6+D14hnuHsxHfxYPGT9NyEHM9UCWOwUhfhH1cln0/NrWdnX V66ESY4ApfGQB5T7+tBHig/6pmUNP3nzrYwwJrsr1WnxJmTe7rZk2CHWdFAbDXcrsi CApyktexW2a3osQ6E86zMBmMO+DiqhQ/mFNV6Spw= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.21] ([90.186.125.58]) by smtp.web.de (mrweb001 [213.165.67.108]) with ESMTPSA (Nemesis) id 0Lxf5f-1jKOTn3rHX-017IAO; Thu, 26 Mar 2020 13:38:52 +0100 To: Levi Morrison Cc: PHP internals References: <59e48d22-6d2d-19df-1b4a-cb2e7281ceea@web.de> Message-ID: <127676cc-c86d-bad9-8501-fbd59975d686@web.de> Date: Thu, 26 Mar 2020 13:38:52 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Antivirus: Avast (VPS 200325-0, 25.03.2020), Outbound message X-Antivirus-Status: Clean X-Provags-ID: V03:K1:0kkvv/ebQt/NUXKCPpkecMbs7BemZutqBuLyzFAW3gb8tjVslOb 7vOvmOgkxdG6a0N6p4PIHnRShq75XuIneInzw+Q5W3gRNomhXMJ4wLKO1fdbqrt6rQMtLiM jWrwXEFyX4+v2lcUcSPW5Vc9JmTx+QBc8GxO2xqR5yahCdxJUV/fcBqnQPuHz5BL8+K1dnb mSe8b/jq//+kxwKqJE2Yw== X-UI-Out-Filterresults: notjunk:1;V03:K0:XQuVRYbMeEI=://lhlf/nOpfvg/pFxH249o l/JEjD9h04l4EIrv4rTJWuu+XLf9IjfZMMkkewykIg3HXN/3dDvtJ9AToGn1x8Ovr3TaEIBNN EUrfuOi6qtjT+Hl/W7h3vQyjLpKA8U9WsKRs993GY9dT8HCQ3tWWyY4lg8U5xHYt8jjALGP0C 4dc6eIlmlZef08qVUk/poDAPz640Htgbrc8AtelU1csB5/EHQK4wiWFqixcgwFDbxGUtX+1Cw nudXrfWwa2wpyUOEg70TL4j8gIVykwsKCyW5AM0MJjXkmYSnZEp9TBMA8NM4Rr0TaxCCGS6t5 GAQ3l/9f2HSF/AXTcvXB0RrtNczyegZs586CsGbliTXFE1T1xB0t+mynZAm/2/UZVunABueng q0jGqBbhlW/BQgmKnBxYUssJzttCmmxdbBdMn0ng3J8J/l9jQytccnWJ+uxOEaUhcm+5Ewk6s Pe5AtemiFyfOtDZva8UWQ6PH/VozQT3+TDXSWEndMXe1u9L6aibBk2pr/IxUMhMSC1rbj4y/7 kn5vb1wYG2f3W8OtRk9iNeqxNdX0q9ToLuBlcI79+EJJiEByQEUpae8yvQ4OjyzzW9Xw8iUHp NxvfN/m9nygXnZXAtu1YarvEKSAEa3JnUGZn3MyGYhRcn3rYO+1EQPWkKF/uVzw35ejpGs1l6 Bwd3O+n3lT9wS16K6rTcailEFUq6uqTKmc+JC9NB+U/mUBe0nSsF8NjrllcUiEoUJ4QFsnq6g 5e1EHyDzEJrmO/Uzx6PcScJa96cUJr74AQ0vskEZo7/lmgh8RlY5BVs/kSlr1LKFLDGS5lbhB OI1eoNOcLOegsWsYHMa8GUSDqc5yFL3B0I8sjyoJFy0bVb9rPRKaD/MVLA5baqiF78t9DBtML Np66TrqH4UW0SXJyYl7G4rWMg5YX/cDdS2WmBBIChhgpZ8xAxKTY4go6ucBY7QmpP8HnfLLk1 U1aY7NizjWgnUQry0AHZj0z/E2XNsvNJjwR5saKydQqN27VhHDZlK1iVAaDn+tWoL4//8eGHQ lCa3vIqEbrO9FAYhBygHHXtJeXBw444No2oAUsPM9R+tIMmUAoFzwQmpH+4OCTEC8iQ3AssYj e76h3qWPUvi01EKA3r+FLogagUhqtAnmrvBc763fC42DIeWqUCcmlPz68IIaADM9sCFObXyIO dTrI8zgovWT1AqhHxdz5jk/gjqxxKMacPkOOMhC3qGRQpgqjZxWrCCLV1vAI0tBaXDwJ0h0la ggOEtVZ0bINX634SF Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Type casting in array destructuring expressions From: enno.woortmann@web.de (Enno Woortmann) Hi Levi, Am 25.03.2020 um 21:44 schrieb Levi Morrison: > To me, this is almost a good idea. However, I would want regular type > checking, not casts. Importantly, regular type checks would fit well > on allowing array destructuring directly in function signatures, which > would basically be a form of named parameters. How exactly do you imagine array destructuring in function signatures with type checks? Something like: function test(['parameter1' =3D> int $parameter1, 'parameter2' =3D> string $parameter2) { =C2=A0=C2=A0=C2=A0 // ... do something with $parameter1 and $parameter2 } Calling the function then may look like: test(['parameter1' =3D> 100, 'parameter2' =3D> 'Hello World']); I guess the type check then depends on the strict_types direcitve whether a call like: test(['parameter1' =3D> 100, 'parameter2' =3D> 100]); would trigger a TypeError or be valid and cast the value for $parameter2 to a string just like a casual function signature with a string parameter. The same behaviour would be applied to array destructuring outside of function signatures. Assumed we gather data from a CSV file: 1,Test,2002 2,Example,2010 3,Demo,2016 And we want to process the data with array destructuring like: $handle =3D fopen('test.csv', 'r'); while (($data =3D fgetcsv($handle)) !=3D=3D false) { =C2=A0=C2=A0=C2=A0 [int $id, string $data, int $year] =3D $data; =C2=A0=C2=A0=C2=A0 // do something with the correctly typed variables } The code would trigger a fatal error when strict_types are enabled. With strict_types disabled it would behave identically as the currently proposed casts. I wouldn't want the example above to trigger a TypeError even when strict_types are enabled. As a form of named parameters regular type checks should definitely preferred over a cast but for the current usage of array destructuring I think regular type checks cover different use cases (which may also exist) than my proposal. Enno