On 24/03/2021 21:26, Mark Randall wrote:
Automatic capture ceased to be a dramatic change from PHP the day
after short closurers were introduced. So they've been a part of PHP
for years now.
I strongly disagree. Short closures say "turn this expression into a
function". You would have to work really, really hard to introduce a
sufficient amount of complexity that scope was hard to follow.
Full-bodied auto-capture means you can introduce a variable on line 1,
and find it's never freed, because it was silently captured by a
function on line 200. Since there's no way to declare a local variable,
you couldn't even opt *out* of the capture, as "let" or "var" do in
JavaScript.
I hit long lists of use() repeatedly, almost always because I need to
perform a series of operations within a callback, prime examples are
database transactions and using throw-aware buffering handlers.
I think a lot of those use cases are using anonymous functions because
they're available, not because they're the best tool for the job.
A transaction wrapper, for instance, doesn't actually need to pass
control to the transaction and back to a callback, it just needs some
before and after boilerplate.
Python has a "with" syntax for this, which calls __enter__ and __exit__
methods on a "Context Manager" object:
https://www.python.org/dev/peps/pep-0343/
Translating the syntax to look more PHP-ish, you'd write something like
this:
with ( new Transaction($db) as $transaction ) {
doSomething($to, $library, $thread);
andThenSomethingElse($author, $title, $library_name, $top_post);
}
The compiler would then insert an appropriate try ... catch ... finally,
with no need for a callback, and therefore no need to capture lots of
variables. The full internal expansion is quite long, so I've posted it
as a gist here:
https://gist.github.com/IMSoP/a4e316684415413dca8ec5debf51a812
This is simpler for the programmer (my example Transaction class is a
total of 20 lines long), more efficient at run-time (no extra stack
frame for the callback), and possibly even simpler to compile (no
analysis needed to work out what variables to capture).
Obviously, this doesn't cover all cases where automatic capture would be
useful, but nor is it just a one-trick pony; the Python spec has lots of
example use cases.
Regards,
--
Rowan Tommins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php