Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121383 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39323 invoked from network); 18 Oct 2023 12:29:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Oct 2023 12:29:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9D1631804BC for ; Wed, 18 Oct 2023 05:29:13 -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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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-oo1-f45.google.com (mail-oo1-f45.google.com [209.85.161.45]) (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 ; Wed, 18 Oct 2023 05:29:13 -0700 (PDT) Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-58111e624bdso4028418eaf.3 for ; Wed, 18 Oct 2023 05:29:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697632152; x=1698236952; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=f6jbPdw0RymP6aV6hiq9wvpUgZ+JikHyJbgbp7penx8=; b=cf6qZeH4ulEmlkPaJLCcWJfRoGUT1eNsCBW2E9NRj8F+HfPg4K6b1sUqRx+e5goGMv BDCvG2TnmMainUaGhvLAdapAd/LUSdtx+A3yHHqERACeG9BFZuJDwW8lBvNiYE1rV/hQ oRgwU2nmuZifrJOdS7WswA8zwVIGw0QVfuG9oSz/vH3lxw3kRehnlxEIsp8No/lwgn+j nyaAHptHexGk4BAo7l3HlvDSPlXkBdIIkqHHX7nPBiRx4CCp+yYbjEqtqaS6Pyj10PTF eubMmI1liL7U9pXTqEsDmoCDwzCQ1mpl1PnnIooU9jpilito8xwmhV6OyYxHUh/+RBtL 8xdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697632152; x=1698236952; h=content-transfer-encoding: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=f6jbPdw0RymP6aV6hiq9wvpUgZ+JikHyJbgbp7penx8=; b=NHU+7gTu/+f40V6D1pvhZ+6ZjM8FUaUNZm7Z3hpSFrvPbG5KBchoCWCJ6kZ4nm58Mu vXJKM0VcIbU4GfryUPEP1MxJQomRs8BqYy06rsTpTZrc/v0h9wQ2QvpZc6lbpwr3oFQJ gVFewge7sVmvi88LV0pFPMRp2CkfBdS39xbctzD+rusD8v37WMZH6/uC5SOadVetImRe GumlHAEEtj5hJybcRt6kS+HTkNgxIVb0E1YB+edSPnVphkc/FZ+zTPuuegR99tqnv0W6 Bogt3kovxgRrMuOUM1Xg6QsCeCmU6WOwvfwB9Ztwk5uy15Fjx3GOZEM9+kJvEAzDtGdy vuUA== X-Gm-Message-State: AOJu0Yz2tPoBm+DQgycTbpJmUXvPacy9vzu0a3kr2XGipT8zGv3LsmY4 jdPzWDfLmw1J4n6mzI3D5sHPwp1mowwDd2YSOcw= X-Google-Smtp-Source: AGHT+IHjRNdjI10a3+S32/olglVdUcZxvbtegh4b7iy1Mj4uokQ6X5hovXLBwqbg/8yr/+SzwaRFqwWnCVTtHbO8bE0= X-Received: by 2002:a4a:e0d8:0:b0:581:e750:9995 with SMTP id e24-20020a4ae0d8000000b00581e7509995mr3637708oot.3.1697632152086; Wed, 18 Oct 2023 05:29:12 -0700 (PDT) MIME-Version: 1.0 References: <173ca550-71a0-4bd4-96f2-b64b6155115a@app.fastmail.com> In-Reply-To: Date: Wed, 18 Oct 2023 14:29:00 +0200 Message-ID: To: Deleu Cc: Brandon Jackson , Saki Takamachi , 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: landers.robert@gmail.com (Robert Landers) On Wed, Oct 18, 2023 at 1:43=E2=80=AFPM Deleu wrote: > > > > On Wed, Oct 18, 2023 at 4:31=E2=80=AFAM Robert Landers wrote: >> >> On Wed, Oct 18, 2023 at 5:26=E2=80=AFAM Deleu wrote= : >> > >> > On Tue, Oct 17, 2023 at 3:43=E2=80=AFPM Brandon Jackson >> > wrote: >> > >> > > > There is also a technique to make the return value `[$key =3D> $va= lue]` >> > > instead of just a value, but this loses simplicity. >> > > >> > > Hmm, since the naming array_first and array_last doesn't clarify tha= t >> > > it's returning a key or a value. What if it returned both as ?[key, >> > > value]. >> > > >> > > That opens quite a few use possibilities: >> > > $first =3D array_first($array); >> > > $value =3D $first[1] ?? throw new Exception(); >> > > >> > > [,$value] =3D array_first($array) ?? [null, null]; >> > > [,$value] =3D array_first($array) ?? throw new Exception(); >> >> >> Hey Marco, >> >> > This function signature can be accomplished by userland once we have >> > `array_key_first()` and `array_first()`. >> >> This would always mean you have to keep them right next to each other, >> it would be a best practice to do so and to split them up should be a >> code smell in any static analysis. > > > "You" (general you) don't always have to keep them right next to each oth= er. Each function is self-sufficient and independent. Maybe on your persona= l bubble you might need to always keep them next to each other, which is wh= y I suggested creating your own userland function that returns key and valu= e together. > >> >> There is no way to tell if a Fiber >> is involved in any function call in PHP, thus if you split them apart >> and call a function, it is possible that your current Fiber is >> suspended and another Fiber mutates the variable you are referencing >> (this is especially true in Classes, not so much in pure functions). > > > I might be completely wrong here, but on my personal bubble, I consider F= ibers to be a corner (a fraction) of PHP compared to non-Fibers PHP. Althou= gh Fibers took the approach to not "paint" [what color is] your function, i= t doesn't mean that Fibers can be used without taking precaution, as with a= ny new tool. > >> >> Since they would always have to be right next to each other, it is >> easier to just combine them into a single atomic function call, which >> would negate the need for static analysis to be involved or surprises. >> >> > It's much better to keep >> > `array_first()` as simple as possible and let everyone build their own >> > approach to go about it since we have so many approaches. >> >> There is only one right approach that prevents Fibers from messing up >> your day, and it would be considerable boilerplate code that you'd >> have to type every time, as well as involve static analysis and watch >> for "people who don't know" better in code reviews. > > > You say only one approach, but a return signature of `: [$key, $value]` o= r the `array_key(array $array, &$key =3D null) ` makes it at least 2 approa= ches that would be fibers-safe, no? > > This is a discussion about an extremely basic functionality that PHP hasn= 't introduced up until now. I think it's an extremely great addition, but r= equires to focus first on the most basic aspect of it. Literally every begi= nner, mid-level, experienced and most senior PHP developers will work with = PHP arrays on way or another. As such, a basic functionality like this shou= ld remain as basic as possible. If needed, PHP can port more helper functio= ns into the core to cater for Fibers in the future. > > In my opinion, the only problem is the ambiguity of returning `null` whic= h might mean the array is empty or might mean the first value is truly null= . If we get a warning box on PHP Docs recommending people to pair this with= `empty()` before using it, it gives users coverage for everything they wil= l need most of the time. Personally, I think I'd prefer the function to thr= ow an exception than to return `null` when array is empty to avoid ambiguit= y and force folks to use `empty()`, but that would also mean complicating t= he function more due to edge cases, which as I stated in this email, I'd ra= ther have the simplest thing possible and let userland fill in the addition= al complexities needed. > > -- > Marco Deleu Hey Marco, > I might be completely wrong here, but on my personal bubble, I consider F= ibers to be a corner (a fraction) of PHP compared to non-Fibers Fibers are part of PHP and are being used in more and more libraries. You may be using them and be totally unaware that you are using them. I'm not saying we need to "cater" to them, per se, but adding two new functions that will (most likely) be used on the same data structure, at nearly the same time, is just asking for devs to get bit by a concurrency bug. Maybe not this year, or anytime relatively soon, but eventually, as Fibers and async PHP becomes more popular. IMHO, it just makes sense to combine them into a single function (however that looks like). You sidestep a whole class of bugs, and documentation/education issues, and make the whole world a tiny bit more reliable. Keeping them separate only looks "simpler" if you only consider first-order effects. That's all I'm saying.