Re: [RFC] Scope functions
| From: | Larry Garfield | Date: | Thu, 07 May 2026 18:26:54 +0000 |
| Subject: | Re: [RFC] Scope functions | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-130799@lists.php.net to get a copy of this message | ||
On Thu, May 7, 2026, at 5:52 AM, Rowan Tommins [IMSoP] wrote:
> For something like "run this in a transaction", the closure is really
> acting like a poor man's "continuation": it creates two new stack
> frames (transaction wrapper, callback) when what you really want is to
> *interleave* the boilerplate and the case-specific code.
>
> In that sense, hygienic macros are probably the "ideal" solution -
> everything is expanded inline into a single scope, and there's no
> Closure object which can be misused.
>
> Context Managers could in fact be implemented as such a macro, and as I
> understand it, the implementation is basically doing that internally by
> manipulating ASTs.
Correct. Absent a macro system like Rust, it's just a one-off compiler "macro" that
turns into try-catch-finally.
> I wonder if there's any "minimal" macro system which would allow this
> kind of inline boilerplate expansion, without needing to design an
> entire meta-language?
The PFA implementation does generate new opcodes at runtime and cache them. So such things are
possible. Wise to generalize or not is an entirely separate question, though. :-)
--Larry Garfield