Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112757 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 93118 invoked from network); 5 Jan 2021 10:25:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Jan 2021 10:25:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id F2BE11804DD for ; Tue, 5 Jan 2021 02:01:51 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 5 Jan 2021 02:01:51 -0800 (PST) Received: by mail-lf1-f42.google.com with SMTP id h22so71308520lfu.2 for ; Tue, 05 Jan 2021 02:01:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=BL3wmf6lfJP0PmfxKd1erlLU+q/peMKBLe4RO6HWUZU=; b=fdeK7mIe5eowcryUjoAMDmLX57svXEjtagmeVE1D8de215WnCJS0dGHvV96cHl/da5 cdo2rTH7Hxqx+g9Vde+hN/bxWZLU5iP8Keph2ZyCkVVTbVm0X2nqE1lphzkjG648/lZO I48DameOG4Ls3pzwQLxj/kFpJX5YC66anpoIUlpXc425dyFs7X9+N+WCnMZGDnKkVrCU n/+sj/UjY70VT/QVkbFIBCm8yscdaw2ANPvJZQ9+AQW0hi8sfs9k+lUkirPsAW5I7OXl snT4G6RoaXljtZ8jpdGY/pdVDVBn6NYqn1gUBwlBpDzLcp9sWmRyT35ZzDzGWVA1KjfF wgBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=BL3wmf6lfJP0PmfxKd1erlLU+q/peMKBLe4RO6HWUZU=; b=QsbLkcamTLYUyqWYsJVrYRpV9FdHw8zVoUHb9xkzNilBe8hUeJZB1vrmkSFFjFfynx Hils1jqVXyO23GrKA9uTwK5SGVQV10rHpRfVgr8eVZQLMgDO5LRwFm0rZtoFVYJi+Fvr 7n7iIoKYdrm5xsKamHoDyP30JVPsHxq9dtpFlFN8CfYm4hWeAmuF8Y6GXTfWyUyYurIJ 9IThax2nNKoVBeXpooDmN7rq/XhpmVvd6oH+p7QFV4A/RqzuLaR1mt636Uq45CZiPPnp jCaar05BcSVIz+JPLIlJVeq82/rRIiJil+OdnfcCTwj+a7W7qtEdGPwbgcJ9ArT09Ljl y5Vw== X-Gm-Message-State: AOAM530/cxXOtINMIiYsYsVA1oNfA9//eH+oCOoqlbMM7shAjIRRPpr7 5x8lqJLGMlNgOu7rO37DZaF7f1lGlHKUfb0xoBU= X-Google-Smtp-Source: ABdhPJxAr1RM+LSyCnCk8LhnnZxrpIWd7NyJzWwxsu0903L1lfoBkGaNZCH2hTtNl18goo4hv/sF8g98+LinqI7LFIE= X-Received: by 2002:a19:8b55:: with SMTP id n82mr32342726lfd.485.1609840909708; Tue, 05 Jan 2021 02:01:49 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 5 Jan 2021 11:01:33 +0100 Message-ID: To: tyson andre Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="00000000000091725505b8244b4b" Subject: Re: [PHP-DEV] [RFC] Add is_list(mixed $value): bool to check for list-like arrays From: nikita.ppv@gmail.com (Nikita Popov) --00000000000091725505b8244b4b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Dec 20, 2020 at 2:43 AM tyson andre wrote: > Hi internals, > > 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 {=E2=80=9C0=E2=80=9D:0,=E2=80=9C2=E2=80=9D:1,=E2=80= =9C1=E2=80=9D: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 =E2=80=9CAny interest in a list type?= =E2=80=9D > https://externals.io/message/111744 =E2=80=9CRequest for couple memory op= timized > array improvements=E2=80=9D > Implementation: https://github.com/php/php-src/pull/6070 (some discussion > is in the linked PR it was based on) > 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 --00000000000091725505b8244b4b--