Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128698 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 6960A1A00BC for ; Sat, 13 Sep 2025 10:56:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1757760886; bh=szjktyvvvYBcD//dvtuisfhLkCWy7jyDnhXUqbRu4T8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=WXgQRQa3r6arWqfJFjmetfHqfr+4IPoWG3QNpSiVBGJj5pDCdE7Ikvsp3lQ3pZN58 esicYTY9hlmx8u1Y1IDX6RVJtettGz1BGijeRy/MVDTbMCM9pj5cD5wNzyUAFGWYvE CWY0QXEHpKiAIa6Cp0EYFgo9ZrRvG9Pp1KLcEJHCFeO5Fwmj0sADBhhWk0ilo1sWa6 Zv9FC4n7m7wTKUXynb9D8uiS5SK0YrIprHocvrqP3Ver9t4UbBa3O7c5WxR7+x9Mso vE15UP2nXM/YrhVW/D74xZICr+qQQT20dheCEUQTXOz/0QtrqvDUJWQT/kdbV5CUXV mjglR3bQCVO9w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7041A180577 for ; Sat, 13 Sep 2025 10:54:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 13 Sep 2025 10:54:36 +0000 (UTC) Received: by mail-yb1-f179.google.com with SMTP id 3f1490d57ef6-ea0150ee46dso2218157276.1 for ; Sat, 13 Sep 2025 03:56:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757760961; x=1758365761; 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=szjktyvvvYBcD//dvtuisfhLkCWy7jyDnhXUqbRu4T8=; b=dDHugf+6rcbrKnO4qJKhKx0PPKv+sYK+pwAT09gyBUgXxQpn62MEsu0iy2bg8lDqr8 pFLmOV7VI01iaxSh/Xt2mPtlUubN9bKJtJewX2WcIbFe+u0egMz7MC0tVa0CXSG8HRKR YEEJago4njuoxY2dVW4HtozGycuAR+P/yh2ctzWhO2wniz1iT6gfFDEdNofTSqvawIoj 4B1C/n7rVLDWxvCG4NJvVay9jNAxqdoDrtuaxcg1gSU4B+Pfc19Ms3TlEis/eL4dHS6U gRbKHxhGn9scvL50FokLVs8aCJNa9VER9QoGRxwToo5Wu4e8eYarxeuDy6B3lg0JjRM0 ikYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757760961; x=1758365761; 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=szjktyvvvYBcD//dvtuisfhLkCWy7jyDnhXUqbRu4T8=; b=rSO8c4G6GM3WGHIynfYxUb2b3BQ3OE5Ifujcoua1YqJ4LOSeE6kpCAyVg0mgT2qO41 IupZQKk9uNlH3x6YfUx9B8/tbXfD94sZ7HSyW4Tl8DLo5sva4N9M2vlVz/w/gnUDzVCy g+eIFWoOn4HunFiPDYew+l+xzniyZqo6PhWIzdXIG9N+m/Hd/B7uFMoOK3m8wVZZiZ8m ndkwZbU0Y/1s2IPeiJWjT0W0elh04/u+FAlYGBrCi34Vdz3aPv9BiMgqUbaothRMxArW Ch4r2ps54YRw4/CR3e+b9gs3Wltp2/exq6BfaGxjXFxOEaeu80G77QY+9LhrMkm5VYQ+ Wvyw== X-Gm-Message-State: AOJu0Yx3pLrNg12up+a5tw/D+7gpbsn3uTDXkJQ/Dy78y64zO2lZqb72 VCuVbBYegTJY/5V/u3oUaLwu41AFXSOGRJXYEIC+ZBxqPZbZPWLoJ27ckfwKmXrZM2fTjeAQsaf AR90XT5tl3oB3utbv3Tlk/bYbTutI2Hs= X-Gm-Gg: ASbGncsN4Pf8vPUnuCmbUazwy1pqOHj8DkaUrchH9xJPBQTe7T3s1y5/AmgClz6qSQ9 mwNVAtS7WOJjvT7yTv2rv6fq/NRN40/OWQY9d54ITg7zkrG5JWRGRlLAzwvj/m8b0reAjwFGKRJ TOtsNkML5zdgWTd8iL2FSd2NzfG8SWPJ+9L4omaBCYQKEcKUA0obpZcx7HNsr1U8OSGe8c5x2dQ jshTx4OQuyC8a8v+8c= X-Google-Smtp-Source: AGHT+IGmc1zTu75RWRkIrLjB7EPg8//ngsA/OZtlTB5hMU8lwSlGRAZgk+TjX5Vl5tbpZeqjeJHLTHuFrOQqSQqMW+8= X-Received: by 2002:a05:6902:20c4:b0:e9d:6a95:bfce with SMTP id 3f1490d57ef6-ea3d9aa9a34mr4816215276.35.1757760960957; Sat, 13 Sep 2025 03:56:00 -0700 (PDT) Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: <23fc3c05-f238-44d5-9a27-778c73db4a23@app.fastmail.com> In-Reply-To: Date: Sat, 13 Sep 2025 13:55:43 +0300 X-Gm-Features: Ac12FXymDDWP-ihi9FMqTprjrNOpgY_eiEOnHaCF60t9L6gIU_J6HI9QKPzsn4s Message-ID: Subject: Re: [PHP-DEV] [RFC proposal] Syntactic sugar for array push() To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net, Ken Guest Content-Type: multipart/alternative; boundary="000000000000ad90bd063eac9dfe" From: drealecs@gmail.com (=?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?=) --000000000000ad90bd063eac9dfe Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Sep 13, 2025 at 1:24=E2=80=AFPM Rowan Tommins [IMSoP] wrote: > On 12 September 2025 20:02:04 BST, "Du=C5=A1an Krehe=C4=BE" > wrote: > >[+] array_push > >[-] array_pop > >[-<] array_shift > > > >array_unshift() is a special case of array_merge(), so it would then nee= d > >syntactic sugar for merging arrays, or rather a reserved merge. > > array_unshift is no more a special case of array_merge than array_push, > particularly in the single-element case we're talking about here - it's > really just the same operation at the other end of the array. > > Since we have an extremely well-established array_push shorthand, so don'= t > need a new one, we could perhaps have "<" for "at start", and "-" for "po= p": > > $foo[] =3D $toPush; > $popped =3D $foo[-]; > $foo[<] =3D $toUnshift; > $shifted =3D $foo[-<]; > > I'm not totally convinced it's necessary, but I don't completely hate it. > I think `$item =3D $list[-]` makes sense instead of writing `$item =3D array_pop($list)`. I don't think we should add shorthand operations for `array_unshift()` and `array_shift()`, as those are slower operations on array, O(n), compared to `array_push()` and `array_pop()` that are O(1). I mean, we should not make it easier to use, a function is good enough. --=20 Alex --000000000000ad90bd063eac9dfe Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Sat, Sep 13, 202= 5 at 1:24=E2=80=AFPM Rowan Tommins [IMSoP] <imsop.php@rwec.co.uk> wrote:
On 12 September 2025 20:02:04 BST, "D= u=C5=A1an Krehe=C4=BE" <dusankrehel@gmail.com> wrote:
>[+] array_push
>[-] array_pop
>[-<] array_shift
>
>array_unshift() is a special case of array_merge(), so it would then ne= ed
>syntactic sugar for merging arrays, or rather a reserved merge.

array_unshift is no more a special case of array_merge than array_push, par= ticularly in the single-element case we're talking about here - it'= s really just the same operation at the other end of the array.

Since we have an extremely well-established array_push shorthand, so don= 9;t need a new one, we could perhaps have "<" for "at sta= rt", and "-" for "pop":

$foo[] =3D $toPush;
$popped =3D $foo[-];
$foo[<] =3D $toUnshift;
$shifted =3D $foo[-<];

I'm not totally convinced it's necessary, but I don't completel= y hate it.
=C2=A0

=C2=A0I think `$item = =3D $list[-]` makes sense instead=C2=A0of writing `$item =3D=C2=A0array_pop= ($list)`.

I don't think we should add shorthan= d operations for `array_unshift()` and `array_shift()`, as those are slower= operations on array, O(n), compared to `array_push()` and `array_pop()` th= at are O(1).
I mean, we should not make it easier to use, a funct= ion is good enough.

--=C2=A0
Alex
<= div>
--000000000000ad90bd063eac9dfe--