Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121372 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 10299 invoked from network); 18 Oct 2023 07:31:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Oct 2023 07:31:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 506331804B0 for ; Wed, 18 Oct 2023 00:31:55 -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 00:31:54 -0700 (PDT) Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-58111e624bdso3910672eaf.3 for ; Wed, 18 Oct 2023 00:31:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697614314; x=1698219114; 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=9wuFpvxJmWR0UHRtob+hWXMm9ljGxP2pIisUNY7cixk=; b=F4muOKXHUZlokY9vLozPWqySAAM/+UbcOk9d3LwkDDaA+Ca1syCFt0YoWJQQgsiA8+ 7+z56RH3QPMisghQSBURUcbox/4wnWNy4hFAb5jezD78oYQwut5M6rlYDmCLYPfyNmLC Ni6GDylvUEGHSI9R62s6OWtybenryFy+6gRBZKtuuYqmG2kikOU3a8cZ09YTFo5kPW9R gy+6SZnAqSJxJGkkvF1eIMmQaFGNWfPS21pgCQGiBkuxb08K1WW5mol34FHfb6aqIirf ku61uiwHGYArNC4DmKHe3UwR0lVMREtYtc/dQGJTVrJG/I1DZjxTgCb9pUxbCsV3PsFs uSgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697614314; x=1698219114; 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=9wuFpvxJmWR0UHRtob+hWXMm9ljGxP2pIisUNY7cixk=; b=RdlsEIOV0XKEUzNUrwe9VuYXP614NVcNMvNilFtHCkH6aBrv4P5es2lv/A3w2iREDP tPzfvLWEnP3u2Y8ynCi8b+guwaJRUh0WeAogqL938y1hHG/VutQ8BzMFEIBleDpMkuLN MVc+K7luzaJcWttADVhK3WGpeQS/6roC3L6PKVVvSpXrgT//5J4dCkUa4FXYIeBedKPy vS8chZq/D3r+55zq5A6bzUN6U8OMzrP8A309bxDMULTutq5aTEfRzjSftoLguSiVB1ot 7Ve+xyMO3nfwsqC04G+PNMBoDpD8eaJORrKp3ugp8ejWt+QXKP+xV2+0+98+78G+4FOo qKZg== X-Gm-Message-State: AOJu0Yx7cLuFXgmH5PKJqgS1MIJpRqZDsLz59Ba4H71A9dt6mMg9jkEX g57epILDlIRnet4UQkzZ1K/NsJzh9+G+9PsnfqE= X-Google-Smtp-Source: AGHT+IH4Aj6N8hsZpsEiS5LeSBmMqiU4ctILZubg6Kk8pnKbcdf53lUi+qbY3Mwlld9+9hJvPA4rVojxo/dOayueeVI= X-Received: by 2002:a4a:ee92:0:b0:57b:63a6:306d with SMTP id dk18-20020a4aee92000000b0057b63a6306dmr3647876oob.6.1697614314005; Wed, 18 Oct 2023 00:31:54 -0700 (PDT) MIME-Version: 1.0 References: <173ca550-71a0-4bd4-96f2-b64b6155115a@app.fastmail.com> In-Reply-To: Date: Wed, 18 Oct 2023 09:31:42 +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 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> $value= ]` > > instead of just a value, but this loses simplicity. > > > > Hmm, since the naming array_first and array_last doesn't clarify that > > 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. 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). 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. Robert Landers Software Engineer Utrecht NL