Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107670 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98933 invoked from network); 24 Oct 2019 23:33:29 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 24 Oct 2019 23:33:29 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 7A4CF2C2B7E for ; Thu, 24 Oct 2019 14:19:58 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: No Received: from mail-ot1-x334.google.com (mail-ot1-x334.google.com [IPv6:2607:f8b0:4864:20::334]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Thu, 24 Oct 2019 14:19:57 -0700 (PDT) Received: by mail-ot1-x334.google.com with SMTP id 94so282090oty.8 for ; Thu, 24 Oct 2019 14:19:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=spa7ch26ClCS2u4ZYKRc5j1WfljFxX1Sut8R7Gv3aBo=; b=GJ6s3T0ol3SkOFTqdPp0YllmfnOIn19h3kanp8O4kBulFO+FCWZhfRYp6TsPrTaEJp ozZDOwG7naEavFxFElGknhE6PSWR6h+4svKnTlXQRt7yJGkFpdxtTwqq4CVPKvXTuGpx eqlQfXao42QybW8dWZEHIowckYvVGWOAfeohkueXmg7pQiGfs9u2/oDEUySQM739vCoQ bgqV+/DIpQV2QVfQl2rmpthNErAStQvjF4kNm5CcBMu8lRD72I7JM+Y/H+bsLCUC2Ksz XgfgXJD5LBeIwtnLALrhoOj+WS8QiCeI+RLM+OwQ5zvaEI0ikAnKXdBpLVUFsfhgE2fj 5YpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=spa7ch26ClCS2u4ZYKRc5j1WfljFxX1Sut8R7Gv3aBo=; b=qO2lCuLo5uf4SY5hbjOzTKrNwsOhLralH4rfWCBpMLrZciS4udoK5z7c9hLtbZi7yU Ljex+N7wdWXLcepJlg6dRofQ8PivFyidfzG9D4MJ+R7MPSYS8pH/D3KjsMnv6Jk4QtYM UwQ7NFEx0ojNRb2FF0ChJyeuaxmQXy90UoaDI5t40gurg1taTEmBouPVeRYl5tSCPa8J HkE4Q6strmpmRnU6cUQ/nxNCIyC41EvYFsOhfwxA13/iv/Qj1KTqwemh8eRqIqkqtiQu Mlg6h5adfQXXK0gMzbtQ59woTJ/7QgOrVgjLzuDzQHKU4KFiHMeGU6NRadQFG1GpnmSU OVMg== X-Gm-Message-State: APjAAAULZ3L0scIY4qgFuHr8y03r8yrA3osmroNWcdTlnEq/ngCm1AnQ n8FeToVfNEzdAEzV4wFtmoedA9ySgmyH1slH8sY= X-Google-Smtp-Source: APXvYqyNbwC5NktoBCE5z+v32kg9xiD6jPpiqipxKXb+XMza59bcX8kQ2lm49rCTK7STWZw9YItUzTxNyoZzDydXmM0= X-Received: by 2002:a9d:2a8:: with SMTP id 37mr8097744otl.163.1571951997051; Thu, 24 Oct 2019 14:19:57 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 25 Oct 2019 04:19:49 +0700 Message-ID: To: Ken Stanley Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000063693f0595ae98d9" X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] anti-coalescing-operator From: webdevxp.com@gmail.com (Kosit Supanyo) --00000000000063693f0595ae98d9 Content-Type: text/plain; charset="UTF-8" Hi Ken I totally agree with Andreas especially: One purpose of the operator should be that you don't have to repeat > the variable. Here you do, e.g. $_SERVER['fname'] But if this operator provide some way not to repeat the variable it will make sense. For example: $_SERVER['fname'] !?? $user->setName($$) But I think this functionality should be of something like pipeline operator: // send tmp variable to the next expression unconditionally $ret = $_SERVER['fname'] |> $user->setName($$); // send tmp variable to the next expression only when $_SERVER['fname'] is set. $ret = $_SERVER['fname'] ?|> $user->setName($$); Also the syntax like above will be consistent with the proposed safe-navigation operators. Cheers On Fri, Oct 25, 2019 at 3:29 AM Andreas Hennings wrote: > On Thu, 24 Oct 2019 at 20:59, Ken Stanley wrote: > > > > On Thu, Oct 24, 2019 at 1:33 PM Dan Ackroyd > wrote: > > > > > On Thu, 24 Oct 2019 at 18:21, Ken Stanley wrote: > > > > > > > > Since PHP 7.0 brought forward the Null Coalescing Operator (??), > writing > > > > more succinct code for how to handle null values has been a blessing. > > > But, > > > > what about the inverse when you want to do something when a value is > not > > > > null? > > > > > > Hi Ken, > > > > > > It may help to give a real world example, rather than a metasyntactic > > > one, as I can't immediately see how this would be useful. > > > > > > People have been expressing a concern over 'symbol soup' for similar > > > ideas. The null colalesce scenario happens frequently enough, that it > > > seemed to overcome the hurdle needed for acceptance. Again, giving a > > > real world example of what you currently need to do frequently might > > > help other people understand the need. > > > > > > cheers > > > Dan > > > > > > > Hi Dan, > > > > After some thought, and searching through my existing code bases, I > believe > > I've come up with a decent code example to help demonstrate the > usefulness > > of the proposed anti-coalescing-operator: > > > > Without !??: > > > > > class ExampleController > > { > > /** > > * PATCH a User object. > > */ > > public function saveAction(int $userId) > > { > > $user = $this->getUser($userId); > > > > if (isset($_SERVER['fname']) { > > $user->setName($_SERVER['fname']); > > } > > > > if (isset($_SERVER['lname']) { > > $user->setName($_SERVER['lname']); > > } > > > > if (isset($_SERVER['mname']) { > > $user->setName($_SERVER['mname']); > > } > > > > if (isset($_SERVER['phone']) { > > $user->setName($_SERVER['phone']); > > } > > > > if (isset($_SERVER['email']) { > > $user->setName($_SERVER['email']); > > } > > > > $this-saveUser($user); > > } > > } > > > > With !??: > > > > > class ExampleController > > { > > /** > > * PATCH a User object. > > */ > > public function saveAction(int $userId) > > { > > $user = $this->getUser($userId); > > > > $_SERVER['fname'] !?? $user->setName($_SERVER['fname']); > > $_SERVER['lname'] !?? $user->setName($_SERVER['lname']); > > $_SERVER['mname'] !?? $user->setName($_SERVER['mname']); > > $_SERVER['phone'] !?? $user->setName($_SERVER['phone']); > > $_SERVER['email'] !?? $user->setName($_SERVER['email']); > > > > $this-saveUser($user); > > } > > } > > Thank you, > > Ken Stanley > > Not convinced. > 1. Most of the perceived brevity is from omitting line breaks and > curly brackets, which is a bit misleading imo. > 2. It is not the intended use of these kinds of operators (ternary or > null coalesce). Normally you would use them to produce a value, here > you use them for control flow only. > 3. One purpose of the operator should be that you don't have to repeat > the variable. Here you do, e.g. $_SERVER['fname'] > > 1. > If you would simply omit the line breaks in the first version, you > would get this: > > if (isset($_SERVER['fname'])) $user->setName($_SERVER['fname']); > if (isset($_SERVER['lname'])) $user->setName($_SERVER['lname']); > if (isset($_SERVER['mname'])) $user->setName($_SERVER['mname']); > if (isset($_SERVER['phone'])) $user->setName($_SERVER['phone']); > if (isset($_SERVER['email'])) $user->setName($_SERVER['email']); > > 2. > Instead of "abusing" your new operator, you could simply "abuse" the > old ternary ?: instead: > > !isset($_SERVER['fname']) ?: $user->setName($_SERVER['fname']); > !isset($_SERVER['lname']) ?: $user->setName($_SERVER['lname']); > !isset($_SERVER['mname']) ?: $user->setName($_SERVER['mname']); > !isset($_SERVER['phone']) ?: $user->setName($_SERVER['phone']); > !isset($_SERVER['email']) ?: $user->setName($_SERVER['email']); > > 3. > One way to not repeat the variable would be to introduce a temporary > local variable, like so: > > if (NULL !== $fname = $_SERVER['fname'] ?? NULL) > $user->setName($fname); > > This gets more useful if the variable expression is something longer. > > A new language feature for this purpose could have an anatomy like this: > https://3v4l.org/TjuuO or > https://3v4l.org/U6arm > > and the short syntax would be like so: > > $product = ($x ??! NULL) * ($y ??! NULL); > > or the NULL can be omitted: > > $product = ($x ??!) * ($y ??!); > > So, the operator would break out of the current expression context, > and produce a value one level up, a bit like a try/throw/catch would, > or like a break in switch. > > This is just a basic idea, it still leaves a lot of questions open. > If the expression context is multiple levels deep, how many of these > levels are we breaking? > > I am not suggesting this is a good idea, but I think it is an > improvement to the original proposal. > > -- Andreas > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --00000000000063693f0595ae98d9--