Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125829 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 qa.php.net (Postfix) with ESMTPS id 75E311A00BD for ; Mon, 21 Oct 2024 21:07:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1729544971; bh=crieaY/YzJ75trx5HsoN9S1PPC93m7NFL8LsZGjs11E=; h=Date:Subject:To:References:From:In-Reply-To:From; b=T2PktlWEX7WfYUp0h7mVO5U4+mR68jKE6h6NL4TcdtAzA1AMu1wtjorTQKz0qAbl2 qwFR7jKdkQ6dkw2GFHYlBihdaEzQ+02j06A7g8QB0SU90+wBpIgrYN4q3x9Qr7VVDo 1cYIyIksnEkQVrXC1nvCKxWq3WAkmJHiGMOSN1PutWrFBTkkhnd6A/t53ht9iraq86 doKgbo9PPWjJ2RHBgkFOnc9ut3eMIktT5DBZoVxF5mlq2ZOwQmrwM6tdvWfsATyC+a wrMw/GRBqle/ADSCwtVnhms5kgAE7RP12MF4voyj0bRNDyl5orD8mx9b2Qa4hcXl3B fygmaKNu8Ze+g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2D8AD18006D for ; Mon, 21 Oct 2024 21:09:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout-a8-smtp.messagingengine.com (fout-a8-smtp.messagingengine.com [103.168.172.151]) (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 ; Mon, 21 Oct 2024 21:09:29 +0000 (UTC) Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfout.phl.internal (Postfix) with ESMTP id 0CF7313801D9 for ; Mon, 21 Oct 2024 17:07:05 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-12.internal (MEProxy); Mon, 21 Oct 2024 17:07:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=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:subject:subject:to:to; s=fm3; t=1729544825; x=1729631225; bh=r27ZGDNvb3QcruS9P8fIviBW/OJ0PKiMeCW2+YhB/BI=; b= ASXxgBhGQz+pVGxgC+FdU1BEO2a/1u6Rkz7dCJmExMR/EVJrPW1z8MvaiQRio9xn kxldPfIa1rQL3N+qK98B6TQXL1ovJHjvtzvngcb3pzO0UF4Tqg16JMWfXLc3TK39 r83huDINLyvxqoGOpEBY6ST+zNragFB6BlgQlzGCNIR/u3//Lx9jJ5IVD8y2Prwj Q53X2BLRhHhQafOCT/kJ8qEQI192drnbMK4fkVtoVCGLeGvVgcIa9fTv1dxqrjP+ vH3aMPYOImf9yZgKGzgEBFfjewq8QA36oMsTITxd1o8uUpX8qBVejyxVRF/UjxRJ RL9LhDldS2O6W26YVtND5Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1729544825; x= 1729631225; bh=r27ZGDNvb3QcruS9P8fIviBW/OJ0PKiMeCW2+YhB/BI=; b=N oLb9HijFYzW/6F7QQ9upr9zoS3kA50nq2NEd7Ltw3Kj2iC7wlqT1nHHPOytvvg/l 8SdreZVUB+IUtZLinfgAi1ERsrSI70wMkwa4UeQiDqx9+W6xf+62/RVMTqJqkE2T BmvGLPRPDrC6PfCZcRFNvYjyd6b3X94A40/9SwPfuPpbLLuD4xXixnZO9AOtfitO 0EZuP8JWiuC8RxtVkyCxg/2ZqcUFWgBNYqRdnqmtDUTMjtitHxKvkTqqqGPX6ZTz xyN9veZy4XXrnD4Vq61HWZXjLBkZ9lY6bFOvybNksiqyHjseghQ4voAQb0BjKZEu g7h5e1ZRIl7jIfEvm5iAA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehledgudehkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecuogfuuhhsphgvtghtff homhgrihhnucdlgeelmdenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertddtvdej necuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdfuceoihhmsh hophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpeejgefgheej veeklefhudfgffetveegjeehtefgteeuieehgeevtdehlefhhfeuueenucffohhmrghinh epfehvgehlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghi lhhfrhhomhepihhmshhophdrphhhphesrhifvggtrdgtohdruhhkpdhnsggprhgtphhtth hopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhssehl ihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Mon, 21 Oct 2024 17:07:04 -0400 (EDT) Message-ID: <555c9d0c-a8b0-44cf-9206-359e5bd7430f@rwec.co.uk> Date: Mon, 21 Oct 2024 22:07:02 +0100 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [RFC] Change behaviour of array sort functions to return a copy of the sorted array To: internals@lists.php.net References: Content-Language: en-GB In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 21/10/2024 14:20, Gina P. Banyard wrote: > This RFC does*NOT* change the by-ref parameter passing*NOR* the in-place sorting of these functions. > I don't see why people are thinking this would be changed, as I only ever talk about the return value. > But I added a sentence in the Unaffected PHP Functionality section. I think maybe people are saying that the RFC *should* aim to do that, in some form. I absolutely agree that the current design is unfortunate; but I worry that bugs will be introduced by people not realising that the function both modifies and returns its argument. Take the classic example of why DateTimeImmutable exists: $now = new DateTime; $tomorrow = $now->modify('+1 day'); // $tomorrow has the expected value, but $now does not And then compare to the proposed behaviour of sort(): $names_as_entered = explode(',', $_POST['names']); $names_sorted = sort($names_as_entered); // oops, we just lost our original order If someone wants to write that code (i.e. make a sorted copy), they'd need to force a clone on the array somehow, e.g. $names_as_entered = explode(',', $_POST['names']); $names_sorted = sort(iterator_to_array($names_as_entered)); Which is probably less clear than the existing version: $names_as_entered = explode(',', $_POST['names']); $names_sorted = $names_as_entered; sort($names_sorted); The same bug can easily hide inside less obviously imperative code. This would remove the highest 10 numbers from a list, leaving others untouched: $result = array_filter(     array: $data,     callback: fn($item) => !in_array($item, array_slice(rsort($data, SORT_NUMERIC), 0, 10)) ); This looks like a safe refactor for performance or readability, but would lose the original keys and order: $top_10 = array_slice(rsort($data, SORT_NUMERIC), 0, 10); $result = array_filter(     array: $data,     callback: fn($item) => !in_array($item, $top_10) ); [Here's a demo: https://3v4l.org/9Nieo For anyone confused by this example, the first version works because the fn() closure captures $data by-value, effectively cloning it.] Regards, -- Rowan Tommins [IMSoP]