On Sun, Feb 2, 2025, at 14:35, Dmitry Derepko wrote:
> 
>> On Jan 25, 2024, at 12:16 PM, Robert Landers <landers.rob...@gmail.com> 
>> wrote:
>> 
>> Hello internals,
>> 
>> Now that throwing is an expression, it allows for some very concise
>> programming. What are your thoughts on making a break/continue into an
>> expression as well?
> 
> Hi! 
> I had similar idea to make `break`, `continue` and `return` be expressions 
> instead of statements to simplify almost the same cases as Robert described 
> above.
> 
> Grammar corrections in the PR. https://github.com/php/php-src/pull/17647 
> Ilija pointed to memory leaking problems as well.
> 
> Thinking about Ilija memory leaking case:
> new Foo + return 1
> 
> I think we may have a workaround here, by allowing all of these constructions 
> only available at some specific points:
> - <point> as the statement now
> - $cond ? <point> : <point>;
> - match ($v) { … => <point> }
> 
> So it will deny such cases:
> - operand OPERATOR <point> (1 + return; $cond && break; etc)
> 
> It may prevent memory leaking problems. Isn’t it?
> 
> I’m writing RFC: https://wiki.php.net/rfc/return_break_continue_expressions 
> I’ll start a new discussion when it will be ready for it.
> 
> 
> ----------
> 
> Best regards,
> Dmitrii Derepko.
> @xepozz

Oh, this was a fun one!

I ended up rewriting the AST during compilation (pass 2) to return/break on the 
next statement instead of in the current statement. That let me get around the 
issue but I guessed nobody would like it and it probably wouldn’t pass on 
technical reasons.

In other words, yes it was an expression on the grammar level, but it was 
compiled as a statement.

— Rob

Reply via email to