Re: [RFC] anti-coalescing-operator

From: Date: Fri, 25 Oct 2019 00:00:46 +0000
Subject: Re: [RFC] anti-coalescing-operator
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-107682@lists.php.net to get a copy of this message
On Fri, 25 Oct 2019 at 01:24, Sara Golemon <pollita@php.net> wrote: > > On Thu, Oct 24, 2019 at 4:55 PM Ken Stanley <dohpaz@gmail.com> wrote: > > > > isset($_SERVER['fname']) && > > > $user->setName($_SERVER['fname']); > > > > > > Will return boolean. > > > > > > > Just testing the waters: Is there any appetite to have AND and OR behave > more like Pythons operator? Similar to javascript || and &&, although not exactly the same. > Instead of now: > (a or b) => bool(true) if either of a or b are true > (a and b) => bool(true) is either of a or b are true > > Behave as: > (a or b) => Value of a if true, b if a is not true but b is, bool(false) > otherwise. > (a and b) => Value of b if both are true, bool(false) otherwise. > > Coincidentally, this change to T_LOGICAL_AND would server OP's purpose. > > I'll tell ya, I'm leaning "No" because BC, but at the same time, AND/OR > seem to be underutilized constructs and I *suspect* (having done zero > research), that most existing uses would yield the same result as they do > now. At one point I started using these operators because they have different operator precedence than || and &&, allowing assignment in conditions without parentheses. I gave up on this only to comply with coding standards and to not confuse people. I would think we will make a lot of people angry if we change this. Imo, let's not. Besides, a lot of this can be achieved with the ?: ternary shortcut (not sure what's the name). > (a or b) => Value of a if true, b if a is not true but b is, bool(false) $a ?: ($b ?: false) === ($a ?: $b) ?: false === $a ?: $b ?: false > (a and b) => Value of b if both are true, bool(false) otherwise. $a ? ($b ?: false) : false === $a ? $b ?: false : false https://3v4l.org/q8Al9 It is a bit more verbose, I admit. But in most cases we don't need to replicate the exact behavior, and a simple ?: will do the trick. -- Andreas > > -Sara

« previous php.internals (#107682) next »