-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: make break
, continue
and return
be an expression
#17647
base: master
Are you sure you want to change the base?
Conversation
I recommend to add some tests with objects implementing |
Unfortunately, this comes with multiple technical challenges that are described in https://wiki.php.net/rfc/match_blocks#technical_implications_of_control_statements. There are also some optimizer issues that are not documented there. It's a very hard problem to solve. |
In kotlin there are In examples: function f() {
while() {
match ($foo) {
'bar' => {
// Returning 'baz' from the *function* (not match)
// This is ok, because match is a standalone expression
return 'baz'; // collapse and free `f()` context
return@match 'baz'; // result of match()
},
};
}
} |
@xepozz Not quite (but it's good to know this exists nonetheless!). You can try this snippet on your branch: class Foo {}
function test() {
new Foo() + return;
}
test(); This will, unless I'm badly mistaken, leak memory. You should see a log in the console if you're compiling with |
Got it
|
I think the main thing which needs to change is basically live temporary cleanup, like it happens whenever an exception is thrown. Possibly FAST_RET mechanisms could be reused, but doing so won't be trivial. But yes, this needs VM support. I don't know about optimizer, it might indeed violate some assumptions there. Maybe not if FAST_RET is just reused - which optimizer knows to handle. |
This is very much the same issue as
I have essentially tried that as of a few days ago: master...iluuu1994:php-src:match-blocks-2 This solves problem 1 and 2, but it still turned out to be much more complex that I was hoping, to the point where my original solution (master...iluuu1994:php-src:match-blocks-var-tracking) isn't much worse. It doesn't solve problem 3 yet, which I will still need to have a closer look at. |
And of course, @xepozz, you should try to come up with actual, useful examples. Something that may be is: while ($cond) {
...
$element = $element->parent() ?? break;
...
}
$value = $cond ? $x : return false;
foreach (...) {
match ($value) {
...
default => continue 2,
};
} But tbh, use-cases seem limited. If they are, the complexity of the solution will be hard to justify. |
Hi!
Long time playing with Kotlin and I like how
break
,continue
andreturn
works there.Tried to implement such features in PHP. Not sure about how correct implementation, but did almost the same as in 0810fcd
I'll try to create RFC a bit later, but now we can discuss the idea.
Yeah, now you can write not only these:
But also
😁