Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays
| From: | Ben Ramsey | Date: | Tue, 22 Dec 2020 02:02:18 +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-112586@lists.php.net to get a copy of this message | ||
> On Dec 21, 2020, at 16:15, Mike Schinkel <mike@newclarity.net> wrote:
>
>> On Dec 20, 2020, at 9:09 PM, Ben Ramsey <ben@benramsey.com> wrote:
>>
>>> On Dec 19, 2020, at 19:43, tyson andre <tysonandre775@hotmail.com>
>>> wrote:
>>>
>>> It can be useful to verify that the assumption that array keys are
>>> consecutive integers is correct, both for data that is being passed
>>> into a module or for validating data before returning it from a
>>> module. However, because it's currently inconvenient to do that, this
>>> has rarely been done in my experience.
>>
>> I think there are some places where
Attachment: [application/pgp-signature] Message signed with OpenPGP signature.asc
is_list() could be
>> unintuitive to
>> those who don’t understand some of the idiosyncrasies of PHP.
>>
>> For example, with
>>
>> $a = ['foo', 'bar', 'baz’];
>>
>> is_list() will return true,
>> but if you run $a through asort(),
>> is_list() will return false
>> because the keys are no longer
>> consecutive integers, but is there any doubt this is still a list?
>> Maybe in a pure sense, it’s not, but I think this could be confusing.
>>
>> But now, if we do
>>
>> $b = array_merge($a, ['qux', 'quux']);
>>
>> $b is now back to being a list, so
>> is_list($b) returns true.
>>
>> While I understand the convenience is_list() provides--I
>> myself have
>> implemented the opposite of this numerous times (e.g.,
>> is_dict())--it comes close to implying a data type that PHP
>> doesn’t
>> have, and I think this could give a false sense of type-safety-ness
>> when using this function to check whether something is a 0-indexed
>> array.
>>
>> Cheers,
>> Ben
>>
>
> Would either is_zero_based() or is_zero_indexed() be a reasonable
> name instead?
>
> -Mike
>
> P.S. See https://en.wikipedia.org/wiki/Zero-based_numbering
I don’t think changing the name changes my concern, but as Tyson
pointed out:
> These idiosyncrasies of php and unintuitive behaviors existed prior
> to this RFC.
I’m generally +1 on this RFC, but as I think about it, maybe there is a
problem with the name. If we choose to introduce a pure list construct
at some point, then is_list() may cause confusion (or maybe it won’t,
if it can be made to return true for a zero-indexed, standard PHP
array *and* a list, whatever that might look like in the language).
Cheers,
Ben
Attachment: [application/pgp-signature] Message signed with OpenPGP signature.asc