Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays
| From: | tyson andre | Date: | Wed, 06 Jan 2021 00:46:30 +0000 |
| Subject: | Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112770@lists.php.net to get a copy of this message | ||
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,
-Tyson
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php