Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112741 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 44062 invoked from network); 4 Jan 2021 03:21:06 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Jan 2021 03:21:06 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D6FFE1804D4 for ; Sun, 3 Jan 2021 18:56:56 -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=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 3 Jan 2021 18:56:56 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 712205C00C5 for ; Sun, 3 Jan 2021 21:56:55 -0500 (EST) Received: from imap26 ([10.202.2.76]) by compute4.internal (MEProxy); Sun, 03 Jan 2021 21:56:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=zu5jHnioT/EVk0zuAOJ5XaiTKm/PCu/5YZrAvGltp 6I=; b=g9xC7nS9bqo57rFtpw9CnghR66bu7diZi1KTryExJZKw0ci+K1oibaRbB e/JFV7AFv4G4AWS19Af0jtP5aHs4jV/iLekfpY6K6m5wtoby2Eghb0jqOIzYOnle LW/qFtXuUDh8yMJmxl5FxzYqxGNZFdy93IY0RzaHD1dh0L2jI7Zjhm2e26u8CSQb 59r3CdFH0EDIcaLqXsLpYL6GSldg5hcbcer+LWG5IBuH6+ZxEhHonZ/iTisESCdj /nwhx3KMVM/o6v2GXMeXIhHvJnWs5KLjbKj+kGDoAGDAHbI7Tzbbyi15sNPDk2j1 88NcOJQNUwtTfrCwEv3KhLfOjwaNg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvdefvddgheduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepleetledthedtteekgfelkeegfefhfeettdehieef keehheetteeffffhheegffdtnecuffhomhgrihhnpehphhhprdhnvghtpdgvgihtvghrnh grlhhsrdhiohdpghhithhhuhgsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfr rghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id C0F2814200A2; Sun, 3 Jan 2021 21:56:54 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.1-61-gb52c239-fm-20201210.001-gb52c2396 Mime-Version: 1.0 Message-ID: <3fe86a3a-7aaa-48c0-b1fa-2f5266e3bdf2@www.fastmail.com> In-Reply-To: References: Date: Sun, 03 Jan 2021 20:56:34 -0600 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: =?UTF-8?Q?Re:_[PHP-DEV]_Re:_[RFC]_Add_is=5Flist(mixed_$value):_bool_to_c?= =?UTF-8?Q?heck_for_list-like_arrays?= From: larry@garfieldtech.com ("Larry Garfield") On Sun, Jan 3, 2021, at 12:17 PM, tyson andre wrote: > Hi internals, >=20 > > I've created the RFC https://wiki.php.net/rfc/is_list > >=20 > > This adds a new function `is_list(mixed $value): bool` that will ret= urn true > > if the type of $value is array and the array keys are `0 .. count($v= alue)-1` in that order. > > > > It's well-known that PHP's `array` data type is rare among programmi= ng 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, bot= h vectors/lists/arrays and hash maps are available) > >=20 > > While it is possible to efficiently check that something is an array= , > > that array may still have string keys, not start from 0, have missin= g array offsets, > > or contain out of order keys. > >=20 > > It can be useful to verify that the assumption that array keys are c= onsecutive integers is correct, > > both for data that is being passed into a module or for validating d= ata before returning it from a module. > > However, because it's currently inconvenient to do that, this has ra= rely been done in my experience. > >=20 > > In performance-sensitive serializers or data encoders, it may also b= e useful to have an efficient check to distinguish lists from associativ= e arrays. > > For example, json_encode does this when deciding to serialize a valu= e 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. > >=20 > > Prior email threads/PRs have had others indicate interest in the abi= lity to efficiently check > > if a PHP `array` has sequential ordered keys starting from 0 > >=20 > > 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 memo= ry optimized array improvements=E2=80=9D > > Implementation: https://github.com/php/php-src/pull/6070 (some discu= ssion is in the linked PR it was based on) >=20 > Due to concerns about naming causing confusion with theoretical=20 > potential future changes to the language, > I've updated https://wiki.php.net/rfc/is_list to use the name=20 > `is_array_and_list(mixed $value): bool` instead. > (e.g. what if php used the reserved word `list` to add an actual list=20= > type in the future, and is_list() returned false for that.) >=20 > I plan to start voting on the RFC in a few days. Possible alternative that's less clumsy: is_packed_array? --Larry Garfield