Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays
| From: | Benjamin Morel | Date: | Tue, 05 Jan 2021 12:26:40 +0000 |
| Subject: | Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112760@lists.php.net to get a copy of this message | ||
> I probably brought this up in a previous thread, but I think it's worth
> considering again here, given recent changes to the RFC:
>
> I think it would make more sense to introduce this as `function
> array_is_list(array $array): bool`. That is, a function that only accepts
> arrays in the first place and determines whether the given array is a list.
>
> Your RFC does mention this possibility, but I think the argument it makes
> against it is not particularly strong, especially given the recent rename.
> The argument is that is_array_and_list($array) is shorter than writing out
> is_array($array) && array_is_list($array) -- that's still true, but with
> the new name, it's really not that much shorter anymore.
>
> On the other hand, is_array($array) && array_is_list($array) cleanly
> separates out the two predicates. If we take into account the fact that in
> the vast majority of cases we will know a-priori that the input is an
> array, just not whether it is a list, making the function
> array_is_list($array) is both clearer and more concise.
>
> function foo(array $array) {
> assert(array_is_list($array)); // Already know it's an array...
> }
>
> if (is_array($value)) {
> if (array_is_list($value)) { // Already know it's an array...
> return serialize_as_list($value);
> } else {
> return serialize_as_dict($value);
> }
> }
>
> Regards,
> Nikita
I like array_is_list(array $array): bool much better, too.
- Benjamin