Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101622 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51055 invoked from network); 18 Jan 2018 23:07:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Jan 2018 23:07:12 -0000 Authentication-Results: pb1.pair.com header.from=andreas@dqxtech.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=andreas@dqxtech.net; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain dqxtech.net from 209.85.215.41 cause and error) X-PHP-List-Original-Sender: andreas@dqxtech.net X-Host-Fingerprint: 209.85.215.41 mail-lf0-f41.google.com Received: from [209.85.215.41] ([209.85.215.41:36813] helo=mail-lf0-f41.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 91/D9-12394-E98216A5 for ; Thu, 18 Jan 2018 18:07:11 -0500 Received: by mail-lf0-f41.google.com with SMTP id t79so13389728lfe.3 for ; Thu, 18 Jan 2018 15:07:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=3jDkpdH8vQmadpc9C53dgLU9VqNgHdiCvo0s1J6QYCc=; b=P2vJKDUSk8zpo4jPSS6gg/ZHWkauwCWZORwi/Mc5usU2WPHTPPWVka02KsvssLJIu1 dkPhGlyierVUd33WjdvbApNjFjbXJnVgW7B8StHQAdO2siBty6w1caq1e8EPJmIe6yXX 1pE12X4OGrkm6EI8JPXWfGJKb7rOT05zGqvAbx0eeRvwirobz+hsrEaZn6o15Nc/ZlV5 UZEozcqXLqhPi5qfyCMaDJaPeN0s8hSDrQEzAbRyhdijeybHr3ghCPpC3S0Xi8FKiiGQ /sjnCxCf2gI+SKPoHDllWqY2fcikzTSBc8LiOgE3iJfCkHnfvBu06WLqKLUnPWOjhmIr e/7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=3jDkpdH8vQmadpc9C53dgLU9VqNgHdiCvo0s1J6QYCc=; b=WGvSuIr8eY+Hx20zlh3wLjrY7IWf4qeuQ/nELB2TtlbSav0AMuxtE9WuURyaD0BIl0 +POuQ16H6iH39H+Z6cjaCIPkMOkPe2jYa9kEtOxsqZh7SCxt+vqLeud8rSAQc+KgYoBG O303PHa6vDZWXL8Sh5rfetEiKh5e03E644bnkF45VffGuTWgt/wN5uPE/TJF0SoGoX24 QDyxbKVBdg61MNBJ38k1ieMMnFpl1a4cV4tcOrYeVhDhZpsARz6HM7IWWUm2kDMc4pFz NYEV7bSpvf2c9wLi+ettyxI8wdDWA5EBOZnuXW5myzgEhToJ4QdbJrdJzMAldR1o3H7J Bcbg== X-Gm-Message-State: AKGB3mJiePQf0PB+WX0NJatDYTazMRoTdmU2mdaocI2xR8/wcjZbD/d5 gorJ/xrg+RExd6h1y68za06XCqUr X-Google-Smtp-Source: ACJfBou5S8tH1TkZ7C953AdQ1LT2RY/kqutXbEWkUw9OjnU8bA6KPhhx3BUyY+0I9eU0XY3kjImLVg== X-Received: by 10.46.114.16 with SMTP id n16mr27944687ljc.14.1516316827472; Thu, 18 Jan 2018 15:07:07 -0800 (PST) Received: from mail-lf0-f42.google.com (mail-lf0-f42.google.com. [209.85.215.42]) by smtp.googlemail.com with ESMTPSA id d82sm1421565lfl.23.2018.01.18.15.07.06 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 15:07:06 -0800 (PST) Received: by mail-lf0-f42.google.com with SMTP id w23so28713757lfd.11 for ; Thu, 18 Jan 2018 15:07:06 -0800 (PST) X-Received: by 10.46.85.66 with SMTP id j63mr3357754ljb.55.1516316826333; Thu, 18 Jan 2018 15:07:06 -0800 (PST) MIME-Version: 1.0 Received: by 10.25.221.20 with HTTP; Thu, 18 Jan 2018 15:06:45 -0800 (PST) In-Reply-To: References: Date: Fri, 19 Jan 2018 00:06:45 +0100 X-Gmail-Original-Message-ID: Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: null coalesce addition assignment operator ??+= From: andreas@dqxtech.net (Andreas Hennings) Some background / personal experience (cross-posting with Stanislav, but I am sending it anyway) I regularly see code with arrays in loops, where the ??+= or ??++ would be nice to have. I do not remember seeing use cases for *=. In all the cases I remember, the left side was an array offset, not a plain variable. I think the spec has to be for plain variables first, even if the main use case is arrays. I only added the ??*=, ??-=, ??++, ??-- for completeness, but I can live without. On 18 January 2018 at 23:58, Stanislav Malyshev wrote: > I think this is taking it too far. If you want language like that, you > always have APL :) It looks a bit cluttery (one character too many for an operator?), but I think it is useful and intuitive. If you understand ??, ??= and +=, you also understand ??+=. But let's see what others say. On 18 January 2018 at 23:52, Andreas Hennings wrote: > The "Null Coalescing Assignment Operator" (or null coalesce assignment > operator) was proposed and accepted in > https://wiki.php.net/rfc/null_coalesce_equal_operator > > I propose even more such operators: > null coalesce addition assignment ??+= (for strings and numbers) > null coalesce subtraction assignment ??-= > null coalesce increment ??++ > null coalesce decrement ??-- > null coalesce multiplication assingment ??*= > > > ## Details > > Each block contains 3 equivalent statements. > > $x ??+= 5; // proposed new syntax > ($x ??= 0) += 5; // syntax with null coalesce assignment operator > $x = ($x ?? 0) + 5; // syntax with simple null coalesce operator > > $x ??+= 'suffix'; // proposed new syntax > ($x ??= '') += 'suffix'; // syntax with null coalesce assignment operator > $x = ($x ?? '') + 'suffix'; // syntax with simple null coalesce operator > > $x ??++; // proposed new syntax > ($x ??= 0)++; > $x = ($x ?? 0) + 1; > > $x ??*= 2; // proposed new syntax > ($x ??= 1) *= 2; > $x = ($x ?? 1) * 2; > > Note that in each case PHP needs to determine the "neutral element" of > the operation. > For string concat this is the empty string. For number addition this > is 0. For number multiplication it is 1. > > > ## Example: > > For me, the most common use case would be something like this: > > $fruitpacks = [ > ['apples', 3], > ['pears', 1], > ['apples', 6], > ['grapes', 22], > ]; > > $totals_by_name = []; > foreach ($fruitpacks as [$name, $amount]) { > $totals_by_name[$name] ??+= $amount; // proposed new syntax > } > > $totals_by_name_expected = [ > 'apples' => 9, > 'pears' => 1, > 'grapes' => 22, > ]; > > assert($totals_by_name === $totals_by_name_expected); > > > ## Notes > > In PHP, the "+=" operator already behaves almost like "??+=", but adds > a "Notice: Undefined offset" or "Notice: Undefined variable", if the > left side is not defined yet. > hhvm apparently does not produce this notice. > https://3v4l.org/l0l0K