-
Notifications
You must be signed in to change notification settings - Fork 7.9k
RFC for conditional break, continue, & return statements #5552
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
Conversation
Zend/zend_language_parser.y
Outdated
@@ -600,6 +601,17 @@ while_statement: | |||
| ':' inner_statement_list T_ENDWHILE ';' { $$ = $2; } | |||
; | |||
|
|||
return_if_stmt: | |||
T_RETURN T_IF '(' expr ')' return_if_optional_return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this grammar, if
without ()
does not work, e.g.:
<?php
function divide($dividend, $divisor = null): ?int {
return if $divisor === null; // return null
return if $divisor === 0 : 0; // explicit return value
return $dividend / $divisor;
}
Is that expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, as currently it is re-using as much syntax that already is known and exists in php. open to discussion though.
This bit of syntax looks very peculiar. Why is this not |
Then why not just |
Good points, I think to further clarify my position, this is the priority, order, and intent I wish the statements to communicate:
Even if these statements span multiple lines, this priority and order remains intact to a large degree. As mentioned in the https://externals.io/message/110107 thread, I am away this is syntactic sugar to some degree, but I feel as proposed, it adds a tremendous amount of value. |
return if
aka "guard clauses"
Sorry to break in but can this RFC be enhanced with throw? Example: throw if (!$user = $this->getUser()) : new AccessDeniedException(); If so, it could be expanded with positive if comparison in future: throw unless ($user = $this->getUser()) : new AccessDeniedException(); One-liners like above would be really nice and readable. |
In PHP 8 you can do this: $user = $this->getUser() ?? throw new AccessDeniedException(); Which is shorter and way less complex. |
int(50) | ||
int(5) | ||
int(0) | ||
string(8) "original" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't forget the newline at the end here :-)
Why not just implement full postfix conditions, like in Perl and Ruby? This will allow (almost) the syntax as requested by this RFC but will be a lot more useful: "fail-fast":
populate values quickly:
Throw errors (like @zmitic) asked:
And if the "guard RFC" will be changed to
|
@ralphschindler Are you planning on pursuing this RFC? |
Closing since there was no response. |
RFC at: https://wiki.php.net/rfc/conditional_break_continue_return
First pass at first class syntactic support for "guard clauses". More technically, a
return if
.Example:
https://stackoverflow.com/questions/5436034/is-there-a-ruby-one-line-return-if-x
https://engineering.helpscout.com/reducing-complexity-with-guard-clauses-in-php-and-javascript-74600fd865c7
https://guidelines.spatie.be/code-style/laravel-php#avoid-else (avoid else)