[RFC Proposal] Null Coalesce Equal Operator

php.internals

Midori Kocak

10 years ago
Hi all, Remember my question about ??= operator? Forgive my rookieness and let me introduce my first RFC here: https://wiki.php.net/rfc/null_coalesce_equal_operator <https://wiki.php.net/rfc/null_coalesce_equal_operator> Let me also include here the introduction I wrote: Combined assignment operators are around since 1970's, appearing first in the C language. Consider the following code $x = $x + 3 that has the ability to be shortened into $x += 3. Hence PHP is a web focused language, ?? operator is used to check something's existence like $username = $_GET['user'] ?? 'nobody'; However due to common variable names are much longer than $username the use of ?? self assignment, creates repeated code. $this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? ‘value’; It is also intuitive to use combined assignment operator null coalesce checking for self assignment. I am sure there would be some language mistakes, please also forgive me for them too. Currently I created a working implementation here: https://github.com/php/php-src/pull/1795 <https://github.com/php/php-src/pull/1795> you can see a terminal working with my implementation also here: http://imgur.com/zLlVFup <http://imgur.com/zLlVFup> I hope I managed to write a simple and understandable RFC. Thank you all for your efforts. Best Wishes, Midori Kocak Computer Scientist & Engineer / ZCPE http://www.mynameismidori.com <http://www.mynameismidori.com/>

Sara Golemon

10 years ago
On Wed, Mar 9, 2016 at 10:14 AM, Midori Kocak <mtkocak@gmail.com> wrote:
> Remember my question about ??= operator? > https://wiki.php.net/rfc/null_coalesce_equal_operator >
As I said before, I support this. The whole thing feels like a no-brainer in terms of implementing it. Davey mentioned on twitter[1] the idea of shortening it to `X ?= Y`, but I think that would be inconsistent with other operators (e.g. *= versus **=) and would complicate further evolutions of syntax such as adding the `X ?:= Y` assignment operator (as shorthand for `X = X ?: Y`). -Sara 1 - https://twitter.com/dshafik/status/707504022224154626

Ryan Pallas

10 years ago
On Wed, Mar 9, 2016 at 11:14 AM, Midori Kocak <mtkocak@gmail.com> wrote:
> Hi all, > > Remember my question about ??= operator? > > Forgive my rookieness and let me introduce my first RFC here: > https://wiki.php.net/rfc/null_coalesce_equal_operator < > https://wiki.php.net/rfc/null_coalesce_equal_operator> >
This looks great! I hope it makes it into the language for sure. One comment though, voting section should not be opened until after the required discussion period.

Jakub Kubíček

10 years ago
Hi Midori, what about targetting this to the next PHP 7.x?
-- Kubis On 10-Mar-2016 12:11 am, "Ryan Pallas" <derokorian@gmail.com> wrote:

Midori Kocak

10 years ago
Hi, I fixed that, corrected some typos and closed voting. Thank you for feedbacks, Midori

Sara Golemon

10 years ago
On Wed, Mar 9, 2016 at 10:14 AM, Midori Kocak <mtkocak@gmail.com> wrote:
> Let me introduce my first RFC here: https://wiki.php.net/rfc/null_coalesce_equal_operator > >
I've jumped on the bandwagon by adding a second RFC related to this one: https://wiki.php.net/rfc/short_ternary_equal_operator which focused on the short-ternary operator with the same goal of creating and assignment version of the operation. We can probably discuss them as a single concept since they relate to the same underlying principle (consistency in binary operations in the language), but I made it a separate RFC in case we want different voting or have different concerns regarding them. If that's a terrible idea, I can close the new one and let Midori add ?: to her original RFC. -Sara

Pierrick Charron

10 years ago
Hi Sara, Just to let you know that I took the liberty to correct the title of your RFC. It was still null coalesce equal operator :) Otherwise I'm +1 for both RFC Pierrick On 10 March 2016 at 22:01, Sara Golemon <pollita@php.net> wrote:

Sara Golemon

10 years ago
On Thu, Mar 10, 2016 at 8:47 PM, Pierrick Charron <pierrick@adoy.net> wrote:
> Just to let you know that I took the liberty to correct the title of your > RFC. It was still null coalesce equal operator :) >
Ooops. Thanks :)

Andrew Faulds

10 years ago
Hi Midori, Midori Kocak wrote:
> Forgive my rookieness and let me introduce my first RFC here: https://wiki.php.net/rfc/null_coalesce_equal_operator <https://wiki.php.net/rfc/null_coalesce_equal_operator>
I think this is a reasonable proposal. I had foreseen that we might add a ??= operator some day when I wrote the original RFC for the ?? operator. I do have one thing to add, though. It's something of a nitpick, but the name ought to be the "null-coalescing assignment operator". This would follow the convention of referring to +=, -= etc. as compound/combined assignment operators[1][2], not "equal" operators (which sounds more like what == and === do, to me) and avoids the mistake ("coalesce" instead of "coalescing") that I originally made in my RFC for ??.[3] I think that RFC naming is important, because the name the author chooses for a feature tends to be the one that ends up in the manual. Anyway, thank you for your RFC! [1] http://php.net/manual/en/language.operators.assignment.php [2] https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#compound-assignment [3] https://blog.ajf.me/2015-12-07-poorly-named-rfcs
-- Andrea Faulds https://ajf.me/