Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121343 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 97535 invoked from network); 17 Oct 2023 09:22:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Oct 2023 09:22:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 02ABA180087 for ; Tue, 17 Oct 2023 02:22:21 -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.1 required=5.0 tests=BAYES_00,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, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 17 Oct 2023 02:22:17 -0700 (PDT) Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-53db1fbee70so9200157a12.2 for ; Tue, 17 Oct 2023 02:22:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697534536; x=1698139336; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=H1tx4fDeLW1tomxE63KhqFhlSg8jGwoeAomErQBNLtk=; b=NUIyjFi14VektNNwL5jWVfdIsVRfdqvGinlGWkhVEg3eV44bpzdsJTHIzWPHaK0dD5 ykPpLOaHtBODtNLIlXnjP3h+4YsuvbWufY7ddkLlFtMamttFf9LFr22bP/U04NyYCvu7 aSxZht45j/V1qrLyKvmO4+Ujs+vg4Vinq7yNvBE8Q4vcOxMTapnjqKPys2OUYbtyRXYY c+AyGMytnCmNMe0swOWG4T+B0OEcHPMHY1ubOVuLScIX1DXpVSEpOkfut40UjzLz4AGB MoHShhvRxp6H7Ebgta1GlNFyHX1CTpnqjVYmh3FCia9if0eKAOdg+WVom8etLjC5CW/j ywag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697534536; x=1698139336; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=H1tx4fDeLW1tomxE63KhqFhlSg8jGwoeAomErQBNLtk=; b=kptvAcnq7ihP62B/LSPOBBUJVRpPtu3/bLxMbZLbHE6j/GqH5DsTVM5dq6Gaemy53M wXg8QyxeSTyVH0PUJX1MGK8fhD6FCp92u/UewQJlrqQsnJa/sb+nV1b3xzVQJky8wxKF cV6vWGrQ8PjB68tERKReGQZ02I7pVjgGGH84lRlcAlsoFC+pgF6zmWzjZws9Pz7jH/PM ya5cAKS7oXnzfVfRCnbAy7Mifd9PGcWICd+tknOE2X1eN+QresZDFBO/3odLK2PATHvU HucRwjynXYA2Sd9uIYAGynTqRHaiI8BViN1Yncj7D3/Ub4npOaW/3urgCUynEgKTo2+u t71g== X-Gm-Message-State: AOJu0Yzc5qDExQnNrwU4Urmt695UqUf9TBv6JsTRaYNALr6PUcChKeKK B1uqVUqCLoGQqItLVW9XSdeD7DGcCaQ794u/VBYm5Y1e2nw= X-Google-Smtp-Source: AGHT+IGgV/8/V4NfGjNpW+Y0MiqQUuTf9aNfI4ox6Icv6z8YvOhK7S6Z5SlxFUTFNJfgqjKmK/0OAHaskGWi9Nfpzp0= X-Received: by 2002:a50:d5dc:0:b0:53e:708c:40be with SMTP id g28-20020a50d5dc000000b0053e708c40bemr1304135edj.6.1697534535898; Tue, 17 Oct 2023 02:22:15 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 17 Oct 2023 11:21:49 +0200 Message-ID: To: Robert Landers Cc: Levi Morrison , David Grudl , internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000000199e60607e60f13" Subject: Re: [PHP-DEV] Two new functions array_first() and array_last() From: kjarli@gmail.com (Lynn) --0000000000000199e60607e60f13 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Oct 17, 2023 at 11:15=E2=80=AFAM Robert Landers wrote: > On Tue, Oct 17, 2023 at 11:10=E2=80=AFAM Levi Morrison via internals > wrote: > > How is returning null any different than $array[0] on an empty array? > https://3v4l.org/ > > > c) Two such functions were proposed and rejected during the > > array_key_first/last RFC > > (https://wiki.php.net/rfc/array_key_first_last) > > > > Yes, that was in 2018. At that time, functions like str_contains() or > > str_starts_with() wouldn't have even come into existence, just because > > there was an obscure way to do it without them. I believe we've moved > > on since then. Today we know how useful it is to use simple, > > easy-to-understand methods, both for programmers who write and read > > the code. > > It's true that sentiment may have shifted in this time. However, a > common argument at that time still stands: `null` is not a good > sentintenal for failure because the value inside the array very well > could have been null. This is not true for the keys. For me > personally, I think I would still vote no. I'm not entirely sure about > that, but that's how I would lean right now. > > As it stands, you'd have to write code along the lines of: > > ```php > $key =3D \array_key_first($array); > if ($key =3D=3D=3D null) { > // handle the failure > } else { > // success > $value =3D $array[$key]; > } > ``` > > Yes, it would be slightly nicer if we could do: > > ```php > $value =3D \array_first($array); > if ($value =3D=3D=3D null) { > // handle the failure > } else { > // success > } > ``` > > But I fear in practice people will just omit the error checking. > > One way around that is to throw an exception. I'm not sure how I feel > about that, but I'll think about it. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > >K3gRs > > Currently, it just outputs a warning, but the result is `null`. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > I'm okay with `null` being given back as it's the current behavior of accessing the key as well, which is often checked through `$array[0] ?? null`. Something like an `undefined` type could solve this problem, but personally not a fan of this in Javascript. Maybe `array_has_first` or something could be made, but honestly I would still just check for `null` myself most likely. --0000000000000199e60607e60f13--