Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109354 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 10931 invoked from network); 26 Mar 2020 21:24:28 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Mar 2020 21:24:28 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 01C451804DD for ; Thu, 26 Mar 2020 12:49:28 -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 12:49:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1585252165; bh=RzKUQRX3oWoPIY8jDB5UU+Y5N9GMfdar+fqvTKMVRMc=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=Lye78tCvHYx2AZxj7pldOtFGW284vgZ4xgexnNv2lOMfDGKbx1lMryU51SrUrM3kK Y15wzppyjHvfyfjFRxqYFE6YMpwecRp3BaYX8wXIiyK+CYUlJn59O5kKuGiqHfySSN s9riNPxEjqZ1Ou012uENg7y2va4SIRIFmsaTg+LM= 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 0M9GYQ-1j62ix1jIc-00Ch9N for ; Thu, 26 Mar 2020 20:49:25 +0100 To: internals@lists.php.net References: <59e48d22-6d2d-19df-1b4a-cb2e7281ceea@web.de> <127676cc-c86d-bad9-8501-fbd59975d686@web.de> Message-ID: Date: Thu, 26 Mar 2020 20:49:24 +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:hMtOOomGloFYzV5YOtJSu1w7B1USA86WlEk4lF/GABjFZdENtOm a3qJPXW66q5dVgYH1tNsNbY2wRlLpgnqUiAJ+AyhxRb/ksPStc5uIiWHr3Y2MVjrG4HMjRp qmvAsa+IKBvdyuMwYXwilkOPsXunvAKZyTIjxsha7ZMjgTZRgb+DvK7X0AnspvnZehiVpvZ magnnvG/dTbFDMZrEzIxg== X-UI-Out-Filterresults: notjunk:1;V03:K0:YNA53CEJn8g=:MbitV6HrbtoaMyt7v6NGEv /exytfU3/EX0t9/CAAI61aQuYQ/Mz5yj0FmWsGbDimigRKhO6uGd6d7ZiWflQOyj6EPLixaxL WpJas7pC8EZ33QDE2UOa4R1uWG9P3yF92oUpaq4azHxejHTLPmcLxMJ7Jb6EUJFUmq2Hcjj0m 7g9T3Ggx8Qtrkd151TIlKzX/9K916q+PIoktBkhyQ3UjQEFrhGe6w7yRFcWFBTavGSP3wOMFy 8ijoy0buLfMP2mXtXUcM15pxEtc7oLlH7bGKf1IisKEv/78RWUei3dHoVa7sebqLeNHFwmOrt RrWx3x3g6gkW6mEiKXXYpY4hZjSzyDf3W9end45knIM30/J6yeue7P8af7sWiNqHRpQsjdFED BZGVBMdmoY+YNTcEqZNbsdFuDWdbNnLdtkAQs5Zc5zBgZ6lrg3AbiMZkO1U1TzHSimJcPXZFe Y8V08oCMDsBr5hWUZQobjAEm9nn2iq2lFcMnbqmMMu+K5pA7QiZ9kNRsr07fgcVul+R5Pyhy8 Dm7WXqUrQ7okuK1aF45r97TMbrGxH+vk6TDeWxCbiBBdY4lOedFQp03YyT57Bc7USuG1nYYzs 54XA8dL+AFZJ0eY7ncFKb2b4vBrZ6ESUq0DIjt/hyZzdR8Eqfo5ZMXeGPiR/hwGcwKS74I4Om HQCoZEDhdpBVh97GbEYHuCCPJdbc7053pDqgjH7dmC+IcnWjDEbSM1rfFVv/N3F5eFTzJt5C2 IiexEClja+dzJMespx9D+PSLmt1fspFR3TLpjzJoFe5ThrKSyXM3gvVym67E5u3u7FfGWI+5J h0T2XO3GPKop1YhPvEcm6lIx4yQqkvFDcxXR+XyD2J9ExehiANTfvYk251gAugIGu3hknUwGB 6TebBzjDJhYZ6LerGKQXkXBciDY2iy1gzeB8LRV+wBqmG2MTuyRsO6jcHS/woGv2xyJojhzD9 gz6fC0vPZeNUZa24PQDIqtsnIWcU5bhhH+aMq+537kTrZoSV2YCxxcbBUNtQKEcDW3pvxJwJn B5P89nu1+IzXjqSyq7RDd0iAb7nT01vhsdCkid84EThMeIbTV9tB2EYZF+MUUepflsQpWgKS3 0fdej4AV8EoKdxtsUn3J+jAaNtIwPhRYR2TeRdMlxQohMqmsX+4pJY4KKr1RKnfhu2oKFfFYB B48fVsJK0xAfO6xWZzOGZ9w97NjqvYXj4Kkm2Y9M0bBDapPPyNFu6JO7y4Y4pGe0EW+LeeYxw 7EFGyD42Om6hlklNh Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Type casting in array destructuring expressions From: enno.woortmann@web.de (Enno Woortmann) >> $handle =3D fopen('test.csv', 'r'); >> while (($data =3D fgetcsv($handle)) !=3D=3D false) { >> [int $id, string $data, int $year] =3D $data; >> // do something with the correctly typed variables >> } >> >> The code would trigger a fatal error when strict_types are enabled. Wit= h >> strict_types disabled it would behave identically as the currently >> proposed casts. >> > > As I mentioned in a previous e-mail, if it followed current strict_types= =3D0 > semantics, it would error if the input row was something like > "42,hello,not-a-valid-year". > > Following standard cast semantics, that input would instead silently giv= e > you a value of 0 in $year, which is often not what you want. Hi Rowan, I liked your idea of strict casts which would increase the possibilities of casting in general but also improve casting while array destructuring. Let's have a look at various combinations. The current proposal (behaviour doesn't depend on strict_types): [(int) $id, (string) $data, (int) $year] =3D $data; "42,hello,2020" --> Works as expected, everything fine "42,hello,not-a-valid-year" --> results silently in a 0. May not be wanted, as you describe, but in my opinion that's fine as it's the current casting behaviour Extending the proposal with strict castings (behaviour doesn't depend on strict_types): [(!int) $id, (!string) $data, (!int) $year] =3D $data; "42,hello,2020" --> Works as expected, everything fine as "2020" can be casted to an int "42,hello,not-a-valid-year" --> Results in a TypeError as the year can't be casted to an int Using regular type checks (depends on strict_typed): declare(strict_types=3D0); [int $id, string $data, int $year] =3D $data; "42,hello,2020" --> Works as expected, everything fine as the id with "42" and the year with "2020" can be casted to an int "42,hello,not-a-valid-year" --> Results in a TypeError as the year can't be casted to an int declare(strict_types=3D1); [int $id, string $data, int $year] =3D $data; "42,hello,2020" --> Results in a TypeError as id and year are both strings "42,hello,not-a-valid-year" --> Results in a TypeError as id and year are both strings My favourite behaviour would be the one described for strict castings/regular type checks with strict_types disabled. But I don't like the idea it's only usable without strict_types to get the casting-feature intended by the RFC. Cheers, Enno