Re: [RFC] [DISCUSSION] Type casting in array destructuring expressions
| From: | Rowan Tommins | Date: | Thu, 26 Mar 2020 12:49:50 +0000 |
| Subject: | Re: [RFC] [DISCUSSION] Type casting in array destructuring expressions | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109334@lists.php.net to get a copy of this message | ||
On Thu, 26 Mar 2020 at 12:39, Enno Woortmann <enno.woortmann@web.de> wrote:
>
> $handle = fopen('test.csv', 'r');
> while (($data = fgetcsv($handle)) !== false) {
> [int $id, string $data, int $year] = $data;
> // 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.
>
As I mentioned in a previous e-mail, if it followed current strict_types=0
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 give
you a value of 0 in $year, which is often not what you want.
Regards,
--
Rowan Tommins
[IMSoP]