Hi folks. Based on the feedback and problems with the previous Context Managers RFC, Arnaud and I have a new approach to present:
https://wiki.php.net/rfc/context-managers-v2
It basically swaps the two-method approach in favor of "it's a generator, you're welcome." The result is, I think, both simpler and more flexible, and resolves a number of other issues as well. More details in the RFC.
There is a linked implementation, though Arnaud warns it's only partially complete so go easy on it. :-) But the design in the RFC is all ready for review.
--
Larry Garfield
larry@garfieldtech.com
Hi folks. Based on the feedback and problems with the previous Context Managers RFC, Arnaud and I have a new approach to present:
https://wiki.php.net/rfc/context-managers-v2
It basically swaps the two-method approach in favor of "it's a generator, you're welcome." The result is, I think, both simpler and more flexible, and resolves a number of other issues as well. More details in the RFC.
There is a linked implementation, though Arnaud warns it's only partially complete so go easy on it. :-) But the design in the RFC is all ready for review.
--
Larry Garfield
larry@garfieldtech.com
Hi Larry,
TLDR: why => rather than as?
As a userland developer I like what this is trying to achieve. I'm not knowledgeable enough about the internals to comment on the finer details or their ramifications - I just have a bike shedding suggestion/question which seemed glaring to me so I wonder if it's because of an ambiguous syntax or some other issue?
The => symbol seems odd to me, given how it's used elsewhere (generally as an indicator of a key/value pair, whether in an array or match block) and how generators/iterables are used elsewhere (ie foreach as)
Your example is:
using (file_for_write('file.txt') => $fp) {
Whereas to my eye this seems like a much more obvious choice:
using (file_for_write('file.txt') as $fp) {
Anyway, just a thought.
Cheers
Stephen
Hi Larry,
TLDR: why
=>rather thanas?As a userland developer I like what this is trying to achieve. I'm not
knowledgeable enough about the internals to comment on the finer
details or their ramifications - I just have a bike shedding
suggestion/question which seemed glaring to me so I wonder if it's
because of an ambiguous syntax or some other issue?The
=>symbol seems odd to me, given how it's used elsewhere
(generally as an indicator of a key/value pair, whether in an array or
match block) and how generators/iterables are used elsewhere (ie
foreach as)Your example is:
using (file_for_write('file.txt') => $fp) {
Whereas to my eye this seems like a much more obvious choice:
using (file_for_write('file.txt') as $fp) {
Anyway, just a thought.
The v1 RFC originally used as, which is what Python uses as well. However, a lot of people were concerned that it implied that the left side expression would be "used as" the right side variable. While in some cases it might be, the typical case is that it's not. The context variable is emitted by the context manager, but is not the same thing.
At the time, no one could really agree on a better keyword. But => was right there waiting to be used, and already has an "evaluates to" or "yields" or "produces" meaning, which is roughly accurate to what happens here.
I don't mind as, personally, but the consensus before seemed to not care for it.
--Larry Garfield