Re: Re: Discussion: making continue and break into an expression

From: Date: Mon, 03 Feb 2025 18:44:21 +0000
Subject: Re: Re: Discussion: making continue and break into an expression
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-126275@lists.php.net to get a copy of this message
On Sun, Feb 2, 2025, at 14:35, Dmitry Derepko wrote: > >> On Jan 25, 2024, at 12:16 PM, Robert Landers <landers.robert@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

« previous php.internals (#126275) next »