Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays
| From: | Nikita Popov | Date: | Wed, 06 Jan 2021 08:58:17 +0000 |
| Subject: | Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112774@lists.php.net to get a copy of this message | ||
On Wed, Jan 6, 2021 at 1:46 AM tyson andre <tysonandre775@hotmail.com>
wrote:
> Hi internals,
>
> > > > 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.
> >
> > After seeing the discussion about confusion with potential
> >
list/vec
> types, the potential for confusion with ArrayObject/SplDoublyLinkedList,
> > and the likeliness that this will usually be called on values that are
> already known to be arrays,
> > array_is_list(array $array): bool seems like a better
> > option - it's
> better to throw a TypeError for objects
> > despite the slight inconvenience of potentially needing to rule out
> null/false.
> >
> > I plan to update the RFC/implementation soon.
>
> The RFC https://wiki.php.net/rfc/is_list and the implementation
> has been
> updated.
> The signature has changed from is_array_and_list($value): bool
> to array_is_list(array $array): bool
>
Thanks, the updated RFC looks good to me.
Nikita