Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102682 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 53345 invoked from network); 10 Jul 2018 06:26:46 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Jul 2018 06:26:46 -0000 Authentication-Results: pb1.pair.com smtp.mail=walterp@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=walterp@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.67 as permitted sender) X-PHP-List-Original-Sender: walterp@gmail.com X-Host-Fingerprint: 209.85.214.67 mail-it0-f67.google.com Received: from [209.85.214.67] ([209.85.214.67:33460] helo=mail-it0-f67.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B8/8C-49043-6A1544B5 for ; Tue, 10 Jul 2018 02:26:46 -0400 Received: by mail-it0-f67.google.com with SMTP id y124-v6so13914420itc.0 for ; Mon, 09 Jul 2018 23:26:46 -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=i/Vt5dlJpDXlpDnxy5n1LVqCb7Y51m3EzULYQNxAiJ0=; b=mAkxxX2I1MHMUtjIhRcA5iHLjJAGwv35YNibCVv4N05pQfDQqVTGV0VnUaqgaknVuu 5nrWEbJbRFL8SOvSyRUNsaMN/upneMLSvpqdp9R4euazbvHwqb+LuSCROeUUSG17f2DN HRZ6BwTe2s6xz4Zd+dWK+vlhai6iTU2u0sg5ECwUGPOoCjZtrxtNjt0wKmAmqoJ6FHef gT37RCStMmIMqGpwFOs8ZvODkIMX+01XMT0XqKYTLSfmTnnBB4hs4lPyKTxU8CCZ+NcH lTlh6Hnz4+5DntrPLYpTiay6b7MrDj0L9ao0Pfy3hjHjlt6HpGFdGJnT6UIKfSZsLAdb nKkw== 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=i/Vt5dlJpDXlpDnxy5n1LVqCb7Y51m3EzULYQNxAiJ0=; b=Yq8kmTsHLhMgvEmVQqVin7cCnRyVy8JARD25AtLffsDlqptH3E02NeR0TE570XYl/U sWRbkcDlps00U5pII2KYXpBsVnP3CrV6yOzaTxubaz33y7x3Sr+oGvY8+hgwl2KQbw3D j+o4YGzNtiumzXdk9kDgWSWRie/bJeKA6O7HrDrvAjvTXjxJJ//A6yd+w2/CRbDDj1hu gGmYhUiQjhaGTHM7F1XQTHxg2XxcVUCTlrX8LK+tp0gQy855JywCV8FRca0eJl3ZlVXM GhPiErcs0GHYIjDkFFfq4r/bfn2R0U98bIN7UGrR/dsXpsL0CXzFH+Z6fH7uf7NxrXro /kgQ== X-Gm-Message-State: APt69E0/6T56oh0RBQWLDuXV2i0wRbdHV8z1RZJ9LATebyQWXRDk4PTN w1oCYGQIu9pUaZvbfXxrdq1K0n41IbYrZ/vJbKM= X-Google-Smtp-Source: AAOMgpef4d0WROMLK5OAHbOmZZDxlmuSPz0lxzjY9KqMZtL4dPH2i2D8WAyTJ/FxXae5qEnoODZ8n3ZC46PfrML15YI= X-Received: by 2002:a02:9b9c:: with SMTP id p28-v6mr18549355jak.28.1531204003475; Mon, 09 Jul 2018 23:26:43 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 10 Jul 2018 00:26:32 -0600 Message-ID: To: Ryan Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000b4b50b05709f363a" Subject: Re: [PHP-DEV] Unifying logical operators From: walterp@gmail.com (Walter Parker) --000000000000b4b50b05709f363a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jul 9, 2018 at 9:03 PM Ryan wrote: > Hello all! Longtime PHP user, first-time contributor to internals (sorry > if I screw anything up)! > > I'd like to propose either the deprecation (7.next - likely 7.4 at this > point) and removal (8.0) of the T_LOGICAL_OR (or), T_LOGICAL_AND (and), a= nd > T_LOGICAL_XOR (xor) tokens, or aliasing them to ||, &&, and !=3D > respectively. > > The behaviours of the two sets of logical operators are very similar (it'= s > incredibly unclear how $x or $y would differ from $x||$y). They perform > almost identically except for the fact that the former set has a preceden= ce > lower that the assignment, null coalescing, and ternary operators[1]. Th= e > page on logical operators[2] states that the reason the two variations > exist is that they "operate at different precedences" (which isn't a reas= on > for existence, but rather a statement of differences). > > Example #1 on the logical operators page[2] gives an example of this > difference: > $e =3D false || true; // true > $f =3D false or true; // false > > Because of the difference of precedence, the second line is evaluated as > ($f =3D false) or true; > > In my mind (and in the mind of every programmer I've spoken to about this= ), > this violates the principle of least astonishment[3]. The assignment > operator is usually thought to be the lowest level of precedence other th= an > parenthesis (as a typical statement would be "do some thing, then assign > its value to this varible"). > > This[4] stackoverflow question sheds some light on the intent of the > alternative operators - they are used for "control flow" in the style of > Ruby's "unless" operator[5]: > > defined("SOME_CONSTANT") or die("SOME_CONSTANT was not defined"); > > However, this behaviour has nothing to do with the difference of preceden= ce > - rather this is due to short circuiting. > > The only difference between the two (unless there are interactions I'm no= t > aware of with the ternary or null coalescing operator) is the precedence > with the assignment operator, causing the return value to be assigned > without respect to the conditional. I ran a quick (possibly imperfect) > script on GitHub's top 30 repositories, and of the usages of the > T_LOGICAL_* operators all but this one[6] operated equivalently to the > symbolic ones: > $gdImage =3D @imagecreatetruecolor(120, 20) or die('Cannot Initialize new= GD > image stream'); > > In this case, the result of imagecreatetruecolor is intended to be placed > in $gdImage, or if it is falsey, die with an error. This could be > rewritten as: > ($gdImage =3D @imagecreatetruecolor(120, 20)) || die('Cannot Initialize n= ew > GD image stream'); > > Or, in my opinion, more cleanly: > $gdImage =3D @imagecreatetruecolor(120, 20); > if(!$gdImage) die('Cannot Initialize new GD image stream'); > That is a matter of style, as I find $a =3D func() or die more clear that t= he version that uses || Not chaining stuff together is a third style. This feels like a Python PEP request. By that I mean that Python wants to have only one way to do any one task. Perl style is there=E2=80=99s more th= an one way to do it. PHP has been a mix of these styles. The big question I have is how much PHP code will break due to an enforced style requirement? Removing them for style seems like it would be a big BC break. Aliasing them might lead to more subtle bugs in legacy code. > > I've written a very rough draft RFC here[7] - and I would love feedback. > If it's taken well I can put it up on the wiki. > > Thanks, > Ryan "Iggy" Volz > > [1]: http://php.net/manual/en/language.operators.precedence.php > [2]: http://php.net/manual/en/language.operators.logical.php > [3]: https://en.wikipedia.org/wiki/Principle_of_least_astonishment > [4]: https://stackoverflow.com/a/5998351 > [5]: > > https://www.tutorialspoint.com/ruby/ruby_if_else.htm#Ruby%20unless%20modi= fier > [6]: > > https://github.com/PHPOffice/PhpSpreadsheet/blob/aa5b0d0236c907fd8dba0883= f3ceb97cc52e46ec/samples/Basic/25_In_memory_image.php#L24 > - likely copied from > http://php.net/manual/en/function.imagecreatetruecolor.php > [7]: https://github.com/iggyvolz/Unifying-Operators-RFC > --=20 The greatest dangers to liberty lurk in insidious encroachment by men of zeal, well-meaning but without understanding. -- Justice Louis D. Brandei= s --000000000000b4b50b05709f363a--