On Sun, Jan 3, 2021 at 8:56 PM Larry Garfield <la...@garfieldtech.com> wrote:

> On Sun, Jan 3, 2021, at 12:17 PM, tyson andre wrote:
> > > I've created the RFC https://wiki.php.net/rfc/is_list
> > >
> > > This adds a new function `is_list(mixed $value): bool` that will
> > > return true if the type of $value is array and the array keys are
> > > `0 .. count($value)-1` in that order.
> > >
> > > It's well-known that PHP's `array` data type is rare among
> > > programming languages in that it supports both integer and string
> > > keys and that iteration order is important and guaranteed. (it is
> > > used for overlapping use cases - in many other languages, both
> > > vectors/lists/arrays and hash maps are available)
> > >
> > > While it is possible to efficiently check that something is an
> > > array, that array may still have string keys, not start from 0,
> > > have missing array offsets, or contain out of order keys.
> > >
> > > 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.
> > >
> > > In performance-sensitive serializers or data encoders, it may
> > > also be useful to have an efficient check to distinguish lists
> > > from associative arrays. For example, json_encode does this when
> > > deciding to serialize a value as [0, 1, 2] instead of {“0”:0,“2”:1,“1”:1}
> > > for arrays depending on the key orders.
> > >
> > > Prior email threads/PRs have had others indicate interest in the
> > > ability to efficiently check if a PHP `array` has sequential
> > > ordered keys starting from 0
> > >
> > > https://externals.io/message/109760 “Any interest in a list type?”
> > > https://externals.io/message/111744 “Request for couple memory
> > > optimized array improvements”
> > > Implementation: https://github.com/php/php-src/pull/6070
> > > (some discussion is in the linked PR it was based on)
> >
> >
> > Due to concerns about naming causing confusion with theoretical
> > potential future changes to the language,
> > I've updated https://wiki.php.net/rfc/is_list to use the name
> > `is_array_and_list(mixed $value): bool` instead.
> > (e.g. what if php used the reserved word `list` to add an actual list
> > type in the future, and is_list() returned false for that.)
> >
> > I plan to start voting on the RFC in a few days.
>
>
> Possible alternative that's less clumsy: is_packed_array?


Hi Tyson,

Thanks for the proposal and implementation. I've wanted a function
that does this on numerous occasions, so I think it will be a good
addition to the standard library.

If I may chime in with Larry's suggestion: I think `is_packed_array`
would  a better name than `is_array_and_list`. The latter is potentially
confusing since it sounds like the value has more than one type.
`is_packed_array` makes it clearer that the function checks whether
the value is an array matching certain criteria. I think it also reads
better when used along with other functions. For example:

    function is_associative_array(mixed $value) {
        return is_array($value) && !is_packed_array($value);
        // vs.
        return is_array($value) && !is_array_and_list($value);
    }

Best regards,  
Theodore
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to