Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121354 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 30936 invoked from network); 17 Oct 2023 16:35:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Oct 2023 16:35:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6ABB0180507 for ; Tue, 17 Oct 2023 09:35:02 -0700 (PDT) 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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout1-smtp.messagingengine.com (wout1-smtp.messagingengine.com [64.147.123.24]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 17 Oct 2023 09:35:01 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id B38233200993; Tue, 17 Oct 2023 12:35:00 -0400 (EDT) Received: from imap43 ([10.202.2.93]) by compute5.internal (MEProxy); Tue, 17 Oct 2023 12:35:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mjec.net; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm3; t= 1697560500; x=1697646900; bh=07UfsQyDJpn1RW5lXE/Pjdr/bNktAzH4b+2 CHb3aBjI=; b=RF7gqP02FR+G3+4+NL4FooduWJwMZeQUz+qD6UnZZAeNITCtpKX tOFUcNfLrOjm3Qlu52fc9/P6H90R3LaR4k04Kqf3rLfb7mSVNxbyAfxaPx1oK7ox iecwFQpEPVi6/LmWu4G/oHSUDMuKM2HRR91v1hh6GXDeTcvo56CFT6fAOIm1kPpo HkOnb9TvpxBLN0iCd6j9y1MmbtIspOZMetrcU3lDRix8oVy6RCsnsT6Rou+2Bc/O 3TvGP4S42thbEYQXPylTTO2VmWwoqlYCU7pQsYB1t6SXRyr5Ejlh39qfHYoepf2X fUJ2IexNK+qURsYRzimmn+hMJehhGgS7hzw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1697560500; x=1697646900; bh=07UfsQyDJpn1RW5lXE/Pjdr/bNktAzH4b+2 CHb3aBjI=; b=pKF1ibRBFvzq+vre0zCOnGTJaPlE9e21cWsj7o8nu5LcrXaaHsJ PPIV7NpmDB+ZWE6ZJ/sBV5bf79TOspGHoI9OUfcXaP7FFKAWjazetmM70NevMoyH 4VM6Xgx52H+CAdBXr6J9cyOOX37GRHa8pC0Eg8owLoPXqqwwnrqnMXDUUNfHbAE9 Oa4yPc5ZZHTaGOCH49PpN/vbPV1fIgD7UI26cM6dxIDXpWAjzuxNnEK7F/9fXJJh rl8smW2EpodXYFYtXod/uZHJAzCKVNwWACqMsI9DtaV0NZK87XGvBdOJMztKcQ7b PJa2PvSukLX22zaw/s3mr5q6LK32oSv/rcg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrjedvgddutddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvfevufgtgfesthhqredtreerjeenucfhrhhomhepfdfo ihgthhgrvghlucevohhrughovhgvrhdfuceophhhphesmhhjvggtrdhnvghtqeenucggtf frrghtthgvrhhnpeeujeffffduudehffetkeefleehvdeuueelledvhfeiueegveffleff ieeluefgueenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpehphhhpsehmjhgvtgdrnhgvth X-ME-Proxy: Feedback-ID: iea414789:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id E55AD2D40087; Tue, 17 Oct 2023 12:34:59 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-1019-ged83ad8595-fm-20231002.001-ged83ad85 MIME-Version: 1.0 Message-ID: <173ca550-71a0-4bd4-96f2-b64b6155115a@app.fastmail.com> In-Reply-To: <689F8F05-7E60-4D47-BE5B-B346891F6F79@sakiot.com> References: <0e05d40c-8574-4fd1-9d73-d6434fc6a2c3@betaapp.fastmail.com> <689F8F05-7E60-4D47-BE5B-B346891F6F79@sakiot.com> Date: Tue, 17 Oct 2023 12:34:38 -0400 To: "Saki Takamachi" Cc: "Levi Morrison" , "David Grudl" , "Levi Morrison via internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Two new functions array_first() and array_last() From: php@mjec.net ("Michael Cordover") On Tue, Oct 17, 2023, at 12:19, Saki Takamachi wrote: >> What about a signature like: >>=20 >> ```php >> array_first(array $array, mixed $value_if_missing =3D null); >> ``` >>=20 >> That would let you specify your own sentinel (or default) where appro= priate, without losing the convenience of this function. > > This is a problem that should be addressed in your application. > > ``` > array_first($arr) ?? $initial > ``` > > I personally think we should make the effort to write it this way and=20 > not have to support initial values =E2=80=8B=E2=80=8Bat the language l= evel. The problem Levi mentioned is that there's ambiguity in this constructio= n: if the valid data domain for elements of `$arr` includes `null`, then= you can't tell whether the null comes from the array being empty, or fr= om its first value being null. In other words, there exists application = code where using `??` in this way is unsuitable. I can think of three approaches for dealing with this, resulting in appl= ication code like: ``` $value =3D array_first($arr, $default); $value =3D count($arr) > 0 ? array_first($arr) : $default; try { $value =3D array_first($arr); } catch (OutOfBoundsException $e) { $value =3D $default; } ``` I think the first of those - what I initially suggested - is the cleanes= t. mjec