Re: Re: [RFC] Context Managers, v2

From: Date: Sun, 22 Nov 2026 03:59:55 +0000
Subject: Re: Re: [RFC] Context Managers, v2
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-133177@lists.php.net to get a copy of this message
On Fri, Nov 13, 2026, at 5:53 PM, Daniel Scherzer wrote: > Some feedback on the current RFC draft > > 1. The behavior section says > >> When a using expression is encountered, the GENERATOR is first evaluated to >> ensure it is a Generator object. If it is not, an Error is thrown. (See the >> section below regarding resources, however.) > > Can you include some mention of ContextManager here, since > GENERATOR can be > evaluated to a ContextManager that is then called to get the > generator. Adjusted the language. > 2. I'm wondering about what happens if the generator throws an exception. > Specifically, it could throw after yielding, but before it terminates, i.e. > when it resumes execution after the BODY finishes, returns, or throws. > > Looking at the "Implementation details" example desugaring, it seems like that > exception would just be silently ignored, but I don't see that anywhere in the > specification. It wouldn't be ignored. It would be treated like any other exception thrown by a generator. If it's inside a try block inside the generator, it would be catchable by any catch block in the generator. If none catch it, then it would propagate up to higher callers like any other exception, possibly triggering a finally block along the way. If the generator wants to handle exceptions it throws differently from exceptions thrown by the context block, it's welcome to do so by having its own exception type/interface to catch. All of this is standard exception handling logic, nothing new introduced. > 3. A number of other things trigger errors, but for generators that yield with > keys, the keys are just silently ignored - did you consider throwing an error > for that too? We considered that quite a bit, actually. The issue is that right now, the keys would do nothing. Checking for a key, however, requires checking if that value is non-numeric, even though it should be the default 0 in every case. That's a small but existing performance overhead. Basically, it comes down to two options: - We assume that no one is going to try to make the key do something in the future, so save the overhead and ignore it. - We assume that someone is going to find a use for the yielded key in the future, so make it an error for now despite the cost, so there's no BC impact in the future. For the moment, we've gone with the first option. If someone has a compelling idea for what to do with the key, now or in the future, that would justify taking the second option. > 4. I'd prefer to not add special syntax for this in try-catch, it looks like it > just saves 3 characters (opening {, closing > };) plus any whitespace that > the developer wants, and it looks a bit odd to me. We can always add this later > if it becomes a common pattern. It also means that the returned value from the > using block cannot be stored anywhere. If someone can figure out a good way to still capture the return value in this case, I'm game for including that now. We just didn't want to pull that thread just yet. Also, in practice, I expect most using blocks won't have a return value. I could only think of 2-3 examples that would, but lots of examples that don't need one. > 5. Speaking of the returned value from the using block, can you confirm that > NoDiscard will be handled properly? Consider a modified version > of the > file_for_writing function given as an example: > > ```php > /** Returns true if everything succeeded, and false otherwise */ > #[\NoDiscard('as failures should be handled')] > function file_for_write(string $file) > { > $fp = fopen($file, 'w'); > if ($fp) { > try { > yield $fp; > // If this line is reached, then the using > didn't throw > an exception > return true; > } catch (\Exception $e) { > log('The file failed.'); > } finally { > fclose($fp); > } > } > // Either fopen() didn't succeed, or it succeeded but > the using > block > // had an exception. > return false; > } > ``` > > for the example > > ```php > using (file_for_write('file.txt') => $fp) { > foreach ($someThing as $value) { > fwrite($fp, serialize($value)); > } > }; > ``` > > the returned value from file_for_write isn't used and > should trigger a > warning. That seems reasonable; I'll have to confirm with Arnaud that we can implement that. > 6. Is there a way for the generator to specify that the *yielded value* should > not be discarded? The current RFC text says > >> If no VAR is specified, the yielded value is ignored. > > and while NoDiscard would highlight the ignoring for a function > return, it > doesn't for generator yields. I don't believe so. The language has no mechanism for that at present. I suppose, maybe NoDiscard could be expanded to mean both the return and the yielded value? I don't know if that's wise... I don't have strong feelings on it, but if someone thinks this is an important feature please suggest a way it could be done. --Larry Garfield

« previous php.internals (#133177) next »