Re: Stricter implicit boolean coercions
| From: | Christian Schneider | Date: | Tue, 26 Apr 2022 15:01:19 +0000 |
| Subject: | Re: Stricter implicit boolean coercions | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117619@lists.php.net to get a copy of this message | ||
Am 26.04.2022 um 15:16 schrieb Andreas Leathley <a.leathley@gmx.net>:
> On 26.04.22 14:47, Christian Schneider wrote:
>> There are two big reasons:
>> - BC: Checking for the truthiness of a value is very common and would require a lot of code
>> changes.
>> - Some of us like the conciseness of "if ($foo) ...", see below
>
> That would not be my target - in an if expression a lot more values are
> allowed anyway (arrays, objects, etc.), so this is not about determining
> truthiness, but actual conversions to a bool type, like a bool
> parameter, a bool return type or a bool property type.
I see, so as long as there are no bool type hints for function parameters everything would be the
same.
This would lead to a minor asymmetry for
$preserve = "yes";
if ($preserve) # Silently working, true
array_slice($array, $offset, preserve_keys: $preserve)); # Not working any more
I assume your solution would be to add an explicit cast to bool? i.e. something along the lines of
array_slice($array, $offset, preserve_keys: (bool)$preserve)); # Explicit cast to silence implicit
conversion
I'm a bit worried about having to keep two different convert-to-bool rule sets in mind
(implicit vs. explicit) and about the additional casts.
Regards,
- Chris