-
Notifications
You must be signed in to change notification settings - Fork 7.9k
RFC Proposal: ??= equal null coalesce operator #1795
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
RFC Proposal: ??= equal null coalesce operator #1795
Conversation
Nice. I support the idea 👍 |
If this is going to be proposed as RFC for php-next, I'd also like to see/propose a shortcut for |
@midorikocak this is awesome! Well done! |
Maybe I'm missing something here, but is |
@Majkl578 Call the shorthand version "elvis equals" perhaps? :) (cf https://en.wikipedia.org/wiki/Elvis_operator for those wondering about the Elvis reference) Either which way, this is brilliant work! |
Definitely needs this! 👍👍 |
+1 |
+1 useful improvement |
+1 |
Here is the RFC on PHP.net https://wiki.php.net/rfc/null_coalesce_equal_operator |
this is a amazing idea +1 On Thu, Mar 10, 2016 at 4:19 AM, Midori Koçak notifications@github.com
From: Email: Phone: Web Designer |
@midorikocak Apart from the point that it should be semantically equivalent to |
zval op1_copy, op2_copy; | ||
int converted = 0; | ||
|
||
if(Z_TYPE_P(op1) == IS_NULL) { |
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.
Missing space after if
keyword.
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.
thanks I will check that.
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.
And it wasn't checked
@krakjoe Am I missing something? I don't get any errors or notices.
|
@midorikocak Add |
@nikic where can I change this for the compiled php in sapi dir? |
@midorikocak It's a runtime setting: |
@@ -1299,6 +1299,7 @@ static void zend_ast_export_ex(smart_str *str, zend_ast *ast, int priority, int | |||
case ZEND_ASSIGN_SL: BINARY_OP(" <<= ", 90, 91, 90); | |||
case ZEND_ASSIGN_SR: BINARY_OP(" >>= ", 90, 91, 90); | |||
case ZEND_ASSIGN_CONCAT: BINARY_OP(" .= ", 90, 91, 90); | |||
case ZEND_ASSIGN_COALESCE: BINARY_OP(" ??= ", 90, 91, 90); |
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.
Hi,
insertion breaks other lines BINARY_OP
alignment.
@midorikocak I still feel like it should be
The equals sign comes before the question mark and I would keep it that way to easier understand that this operation can potentially change the value of I'm not sure if I love the idea though, so whichever way it gets implemented, no doubt it'll be very useful. |
@ZurabWeb Compound assignment operators always have the operator on the left side, e.g. @midorikocak There is some info about writing tests here: http://qa.php.net/write-test.php However it's probably easier to simply pick an arbitrary file in Zend/tests and follow it's structure :) |
There will be a notice if variable on left side does not exists? |
@midorikocak needs a rebase. |
Why was this PR closed ? AFAICS the RFC was approved and listed in the pending implementation list. |
The author closed it, I guess she lost interest. Anyone should be allowed to take over I guess. |
@saramg intended to take it over as there was an issue with this patch that was tricky to resolve. |
@Fleshgrinder @dshafik Thanks for your response. @sgolemon Care to give a status update ? 😍 |
If someone wants to give this another go: And: |
Wouldn't
|
I don't agree with that. In that logic, the |
Why? There is no I think a compounded op is working like |
@jfcherng Also, this is not a thread for discussing the style or so. This discussion has already happened and has been accepted. This thread is for implementing what has been decided on. |
Why did this not make it into 7.2? |
Unfortunately the implementation was never finished/fixed... |
Guess I'll update the wiki page then - @lkmorlan may have jumped the gun a bit. |
https://francescocirillo.com/pages/anti-if-campaign |
I'm interested in taking this up and finishing it, is there anything in particular missing apart from tests? |
@passcod Quoting part of a mail I wrote in one of the discussion threads for this feature:
The last line is the TL;DR, those are the issues that need to be solved. Additionally there is the question mentioned earlier where |
Right, so the first order of business on this is to specify exactly the behaviour of the operator, which will then inform the patch (and its feasibility). I'll spend some time thinking about this. Am I correct to assume that the best place to discuss what I come up with would be the mailing list, or would this pull request thread do? |
What's the status here? Note that feature freeze for PHP 7.3 is scheduled for July, 17th. |
Unless someone else has made progress, I don't think it will make it. |
|
I am a little worried that this is proposed as a syntactic sugar for
(Note: I am puzzled that the latter idiom works as described given that |
Just to note (in case it wasn't obvious) that I'm no longer onto this. While I hoped to have enough time, it's a lot harder than initially envisioned, and now I'm full busy for the foreseeable future. Hopefully someone else can pick it up :) |
We should definitely repoen this one. |
I am thinking that we should limit the operator to simple data types instead of callables or resources that can change on runtime. We should flag an error. |
I've created an implementation for |
Actually this was a question about null coalescing operator. Check this:
I want to check if some var is null and if the same var is null set the same var to ‘value’.
Hence I am repeating the same variable after the equal operator, this does not feels right.
So I feel that we need another operator like “??=“ similar to +=;
$this->request->data['comments']['user_id’] ??= ‘value’. So if the var is null it’s set to ‘value’ and else stays the same.
In this pull request I tried to implement this.