Re: Type casting while array destructuring
| From: | Rowan Tommins | Date: | Tue, 24 Mar 2020 14:23:52 +0000 |
| Subject: | Re: Type casting while array destructuring | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109273@lists.php.net to get a copy of this message | ||
On 24 March 2020 13:32:35 GMT+00:00, Enno Woortmann <enno.woortmann@web.de> wrote:
> How about adding some syntactic sugar and
>allow type casting inside the detructuring expression?
>
>$data = "foo:*:1023:1000::/home/foo:/bin/sh";
>[$user, $pass, (int) $uid, (int) $gid, $gecos, $home, $shell] =
>explode(":", $data);
On the one hand, this seems fairly straightforward, and although casts wouldn't normally appear
on the left side of an expression, the intent is pretty clear.
On the other hand, this is exactly the kind of thing where strict_types=1 makes things worse -
you'll actually get *better* error output if you use strict_types=0 and pass the string to a
function marked as requiring int. By forcing a cast, you're actually silencing the error, and
turning all unrecognised values to 0.
If the desire is for stricter type handling, we probably need stricter casts, perhaps using
different syntax like (int!) rather than just more places to put the existing ones.
Regards,
--
Rowan Tommins
[IMSoP]