Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129251 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 0EF4E1A00BC for ; Sat, 15 Nov 2025 23:11:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1763248312; bh=1Tz9e0Lp0OULufqevd93zSqodey2S+y1gfEdut9bBjU=; h=Date:From:Subject:To:From; b=b7OVOvfymfyKpSmWRsuvUMjjgnWckwUSK5s83soo5+cRK3OPl0vqSVPI27ybcJii6 m2MNUhcHdhYG1aTtZtW2EjtQfzAYaOa/F9kFk+M/OzfuBdbcMuYmEFYNbkAadJpWGp lksoPzZsj/ywFfk36r3dLXWXVmb4ZMStNbvNzN+UfEdliKbSHsNKRCWFDV55UTC0vB jtaVb+MBMMJuigWZYtvsxhzx3Y7Tg0KWP7MVegrdOFzHKE/BDvObvfYoG2cZxdIFA7 m1DeZ8irdSC7ocSnSbJl4vX85AOa4w32mqfCwMvy16XdS6P7q+Wm6aC0SlT4eDiDSt vqgpPBbrdwrFA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 351EC1805BF for ; Sat, 15 Nov 2025 23:11:51 +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-a3-smtp.messagingengine.com (fout-a3-smtp.messagingengine.com [103.168.172.146]) (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, 15 Nov 2025 23:11:50 +0000 (UTC) Received: from phl-compute-11.internal (phl-compute-11.internal [10.202.2.51]) by mailfout.phl.internal (Postfix) with ESMTP id 5734AEC00A7 for ; Sat, 15 Nov 2025 18:11:45 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-11.internal (MEProxy); Sat, 15 Nov 2025 18:11:45 -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:message-id:mime-version:reply-to:subject :subject:to:to; s=fm3; t=1763248305; x=1763334705; bh=rG8y4vnxTS MpJ61f4rSAwLROk42I8W7TDIkZFh+H328=; b=odw0SRP/YjVdMGllT4zdjbseCz zdAfyTNKUiLm5iI7ECygJRNvsaxy7hvgPNly3n28565mhXYBYfvpaEC7/hVZzycd 0sjOi73pkTKCqOwhj1NSMjWGq9IET4UE/fUAllTD5d2NFJnynSIHLhBUD9cRJjP+ /oU3BlGsHY/LViNFLWMhPfTYTVAEKwnf4zCdmpZa/HfsQWxDPPbfRSTts0TgivnZ DF1fE2T5ptRD4rOff8uKYSha7Yy5j8WHnZgcnMekkJ7HEUQaq5A4TlbZxj0tFJPs SJ0bgnlWE+QQZ10J7CXQr497C84x2YkDBN9byG+uYt7QDz8Pqij5FL30K8EA== 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:message-id:mime-version:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1763248305; x=1763334705; bh=rG8y4vnxTSMpJ61f4rSAwLROk42I8W7TDIk ZFh+H328=; b=K3kuk0ZyZPX9zLj7Z4vEqk0yZEkR9PjSKEu8kGEDRmFk27nQJpD PfeQxZh+KWck+01Vmx+dWwijIHafuwiHCCqG6bTyt2W6K/5Kj+F7xJOTWn8Oks7V gSE65nS3IwZF6FNM0Y9BOEj/dXiwIRPorQZkXU1nE8d132wWdZAZIm0Gp26M5tp7 X6RnGSPM8RAIHyGDek3XAyTgKcVB4tnD5KZpUWWyfAjvxDKW66iGgezJw/JUI9Al UATw90gB7wgGwqj3pr1Pjk7OcXslnayMtOGNefjWQ2PdKWQy5MbC83fRNOV1a1b4 jkSxix9QK/r5hk3xdgH3B0KgEpJfs/FfXmA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddvudefleelucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfhuffvtgfgsehtkeertddtvd ejnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdfuceoihhm shhophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpeeiiedule ejhfdtleethfeghfdvhfeiueehjeevleefgfegfeeigfejveetheelfeenucffohhmrghi nhepghhithhlrggsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomhepihhmshhophdrphhhphesrhifvggtrdgtohdruhhkpdhnsggprhgt phhtthhopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlh hssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sat, 15 Nov 2025 18:11:44 -0500 (EST) Message-ID: <26a2f13c-f318-4d6c-9595-bfaaebcbabcb@rwec.co.uk> Date: Sat, 15 Nov 2025 23:11:44 +0000 Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-GB Subject: [PHP-DEV] Examples comparing Block Scoped RAII and Context Managers To: internals@lists.php.net Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") Hi all, The Block Scoping RFC and the Context Manager RFC cover a lot of similar use cases, and a lot of the discussion on both threads has been explicitly comparing them. To try to picture better how they compare, I have put together a set of examples that implement the same code using both features, as well as some other variations, in this git repo: https://gitlab.com/imsop/raii-vs-cm A few notes: - The syntax for the two proposals is based on the current RFC text. If they are updated, e.g. to use different keywords, I will update the examples. - I have included examples with closures which automatically capture by value, since a lot of the same use cases come up when discussing those. - There are many scenarios which could be included, and many ways each example could be written. I have chosen scenarios to illustrate certain strengths and weaknesses, but tried to fairly represent a "good" use of each feature. However, I welcome feedback about unintentional bias in my choices. - Corrections and additional examples are welcome as Merge Requests to the repo, or replies here. With that out of the way, here are my own initial thoughts from working through the examples: - RAII + block scope is most convenient when protecting an existing object which can be edited or extended. - When protecting a final object, or a native resource, RAII is harder to implement. In these cases, the separation of Context Manager from managed value is powerful. - Context Managers are very concise for safely setting and resetting global state. RAII can achieve this, but feels less natural. - An "inversion of control" approach (passing in a callback with the body of the protected block) requires capturing all variables *not* scoped to the block. Even with automatic by-value capture, those needed *after* the block would need to be listed for capture by reference. - Building a Context Manager from a Generator can lead to very readable code in some cases, and closely mimics an "inversion of control" approach without the same variable capture problems. I would be interested in other people's thoughts. Regards, -- Rowan Tommins [IMSoP]