Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129482 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 D6DF51A00BC for ; Mon, 1 Dec 2025 15:38:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1764603526; bh=7FcUqWn5qmmox80O2XBpR51ufWiKCF7wUK+JufkPQSo=; h=Date:Subject:To:References:From:In-Reply-To:From; b=kChJ6Bk+rPuyifANwVUJv7xfVPFz1/QLQtT4hhzZZmBqdYQZrrPFoDMZT6H643Jgy RYiGdYtM8kDzb5NtFx4F4DNUVQBMQTh9akGRkCj/EiY6ZxYeShwkaUX/m1q83xm8Aj HjhWY1/fVJIimkpMavvAEb1j266cUmHc14pPHipp9s+/1gFNNWg7byYRBQ5A+OM7JC ZzFTshswmktm64kLHyHclcvaKgH/kxGMI2vkr43WPS6wKYSO6+BAjujud5hUl0VGaY mfBbO3dMNEuZR8vvs7D4RAWADYsZnpa1RnrQSfzesT45BR0EUma5k5cjOY2X9QfD3/ tGGp/ogWLtt3Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 831E018004C for ; Mon, 1 Dec 2025 15:38:45 +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=-2.8 required=5.0 tests=BAYES_00,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.1 X-Spam-Virus: No X-Envelope-From: Received: from fout-a2-smtp.messagingengine.com (fout-a2-smtp.messagingengine.com [103.168.172.145]) (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, 1 Dec 2025 15:38:35 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id B87CEEC0790 for ; Mon, 1 Dec 2025 10:38:29 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Mon, 01 Dec 2025 10:38:29 -0500 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=fm1; t=1764603509; x=1764689909; bh=IciLYlCCdIe2VUIHh3Ga2LouezOL5a1Cqslz/kE2cEE=; b= lEcugcI6WrIgyji6dHsCZidTfrf9diXrVPs3jVjSuYtZpe2BDDC/HehksochUG7a SRuYuU9PVixQYZsY1z9LnlAgVyNxn2N6FKh+YRA4EyI1Qo81ZOtLbsmIhMVEvSBT a2thMvnM0rtJDcUpkErMfkP8MJdX3yxuXfpep+xcVtvc1TtEwlt6j4zNMJjTRNX8 qVIV+bQtmBjpck+dojY+US7iwLAphCDvvtPdXKWnd13WGRPOB2WcMDKY+G6XAw/j 4yXWxQvgwG+zT/rOA6seMsKHiPD+MIn6R2jAXFYJtQv+dEqP7vhZeuKwF7ilnyxJ tR3+rAqurrKHQUj9vlHK5w== 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-sender :x-me-sender:x-sasl-enc; s=fm1; t=1764603509; x=1764689909; bh=I ciLYlCCdIe2VUIHh3Ga2LouezOL5a1Cqslz/kE2cEE=; b=QB8MV4NA6PKR8aaEp O06+cOoKvPmEXxohit1Vzc4O7z8iu9mScsv6ioJEzTC9uhtVv5Q30JQ22aw/uEnT /HADxKre7FbVHjBK3lM7bLA8MRGO2O6pXhm0+EMOaiQhVGHQ37PiIa6zkTqwmWt/ lAs5WOUsOqkfwaR3ASZDrEY5WoThSQf76/vL1p0CD/hxRZFSyAOS5XzKaR+xAqJl p/PJBbHFN0372MTTy6rUZdPZ9MAI7uQjVyDXYv3Lclgwu03QOu5eJhHdDIV2ZNE1 bPRHF6BQWGPcIImcpHorOBhM7DFs147VgTzaQ1cShqze/vopeMqDcC44rp/ECFrX NpAAg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddvheektdelucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertd dtvdejnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdfuceo ihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpeffke evudffuddvheejvdefkeelfedtudegfeehjeduheegieduffeggeegveefheenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprdhphh hpsehrfigvtgdrtghordhukhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphho uhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Mon, 1 Dec 2025 10:38:28 -0500 (EST) Message-ID: Date: Mon, 1 Dec 2025 15:38:27 +0000 Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [RFC] Context Managers To: internals@lists.php.net References: <74abada6-cb34-490e-abeb-517e39b3ffa0@app.fastmail.com> 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 01/12/2025 14:26, Tim Düsterhus wrote: > You are wrong here. Manually interacting with an Iterator is not just > safe, it is also necessary for some use cases. The simplest example > would be iterating two Iterators in lock-step (i.e. doing a `zip()` > operation / array_combine()). It is certainly *possible* to use the Iterator methods safely without a foreach() construct or built-in aggregate; but it's also easy to miss the implications, and cause very confusing behaviour. For instance, you could call next() in two different pieces of code, and each would miss half the items; or you could call rewind() on an iterator that was already in use elsewhere. The same is true of the ContextManager interface: you could certainly call enterContext() and exitContext() manually with no problems at all. It might even be necessary, for much the same reasons as iterators: you might want to write code that aggregates two context managers in a specific arrangement. As I understand it, your concern is that someone will call enterContext() without exitContext(). You could do exactly the same thing if you manually handle an Iterator. Consider this method: function getDataIterator(): Generator {     $this->acquireLock();     while ( $this->hasMoreResults() ) {         yield $this->getNextResult();     }     $this->releaseLock(); } If the user calls $it = $foo->getDataIterator(), they can call $it->current() and get a result, but never call $it->next(). They could even use it in a foreach() loop but `break` out before consuming all the results. In such cases, the lock would never be released, unless there's an extra safety check in the destructor. I think the currently proposed names actually make that *less* likely for a ContextManager: if you enter something, you probably want to exit it later. We could reinforce that further by also prefixing the method names with "__", but we don't have any precedent for that, and misuse would still be possible. -- Rowan Tommins [IMSoP]