Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70427 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95401 invoked from network); 26 Nov 2013 19:47:21 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Nov 2013 19:47:21 -0000 Authentication-Results: pb1.pair.com header.from=petercowburn@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=petercowburn@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.171 as permitted sender) X-PHP-List-Original-Sender: petercowburn@gmail.com X-Host-Fingerprint: 209.85.214.171 mail-ob0-f171.google.com Received: from [209.85.214.171] ([209.85.214.171:48590] helo=mail-ob0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A2/6B-39355-8CAF4925 for ; Tue, 26 Nov 2013 14:47:20 -0500 Received: by mail-ob0-f171.google.com with SMTP id wp18so6350801obc.30 for ; Tue, 26 Nov 2013 11:47:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=h/PDgRcc8RNqUho3GnOj0x2jTgGA+AQ3PEP5TVNx+6k=; b=CYVkmpECiqdxcEVL3rIIm+lJV/xdV53E4fs/ZqQhUUosfLxY21frI8P3VwxYFbAmJd oUlde06TeQgUjNbLHaqOc0TsSFAqiHZ/4XZsPweEuTCFv0dxQ5LiFeTzpGHyjQ1Qn568 V9VGjFvk/ZBbOjMq1NDUxhedTOSSCWqq2AMVl9Nm/HT2NS7dXnmdj3lA68SJQ/NfE8/N K7l9SdQIti6A9ru4B9BWbrV61HFYzpDQJljeI/E4QtUHZwGSsoR/GxpKja4TedZ/Z21X Aq1qoXOGHEyWfunMI48CWdXf5uSH+VH3/7+7j4KLbLBQ6+Q/4bYB1fAXaXfVXGMvOKOM r/QQ== X-Received: by 10.182.246.7 with SMTP id xs7mr2128991obc.81.1385495237272; Tue, 26 Nov 2013 11:47:17 -0800 (PST) MIME-Version: 1.0 Received: by 10.76.23.98 with HTTP; Tue, 26 Nov 2013 11:46:36 -0800 (PST) In-Reply-To: References: Date: Tue, 26 Nov 2013 19:46:36 +0000 Message-ID: To: Jezz Goodwin Cc: PHP internals Content-Type: multipart/alternative; boundary=001a11c2fa0674ae1104ec19bf36 Subject: Re: [PHP-DEV] [Proposal] Modification to ?: functionality From: petercowburn@gmail.com (Peter Cowburn) --001a11c2fa0674ae1104ec19bf36 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On 26 November 2013 18:35, Jezz Goodwin wrote: > Hi, > > Before mentioning anything about new syntax, I thought I'd point out that > this code is already possible for setting defaults: > > (isset($foo)) ?: $foo =3D 'default'; > > or > > isset($foo) ?: $foo =3D 'default'; > > (I personally prefer wrapping the condition within brackets) > > Saying that, I do think a syntax for setting defaults which doesn't invol= ve > typing the variable name twice would be nice. > > My favourite one listed so far is: > > $foo ?=3D 'default'; > I like that too. Elaborating on that idea, here=92s one idea from a while back, making much wider use of a =93?=94 operator=85 https://gist.github.com/salathe/3256826 > > > -- > Jezz > > > On Tue, Nov 26, 2013 at 6:23 PM, Philip Sturgeon >wrote: > > > On Tue, Nov 26, 2013 at 1:08 PM, Etienne Kneuss wrote: > > > > > > > > > > > > On Tue, Nov 26, 2013 at 6:57 PM, Philip Sturgeon > > > > wrote: > > >> > > >> On Tue, Nov 26, 2013 at 12:41 PM, Paul Dragoonis > > > >> wrote: > > >> > On Tue, Nov 26, 2013 at 4:58 PM, Chris London > > wrote: > > >> > > > >> >> On Tue, Nov 26, 2013 at 8:15 AM, Mats Lindh > > >> >> wrote: > > >> >> > > >> >> > On Tue, Nov 26, 2013 at 3:43 PM, Chris London > > > >> >> > wrote: > > >> >> > > > >> >> >> I believe these two statements are functionally equivalent: > > >> >> >> > > >> >> >> $foo =3D $foo ? $foo : 'default'; > > >> >> >> > > >> >> >> $foo =3D $foo ?: 'default'; > > >> >> >> > > >> >> > > > >> >> > They are. > > >> >> > > > >> >> > > > >> >> > I would like to change it so it also checks for isset() so I > > propose > > >> >> > the > > >> >> >> following would be functionally equivalent: > > >> >> >> > > >> >> >> $foo =3D isset($foo) && $foo ? $foo : 'default'; > > >> >> >> > > >> >> >> $foo =3D $foo ?: 'default'; > > >> >> >> > > >> >> > > >> > > > >> > We can't change the behaviour of ?: that ship has already sailed, > > we'll > > >> > just be hurting existing codebases on upgrading and more user > > education > > >> > is > > >> > required to understand non-visual changes in syntax like this, let= s > > try > > >> > to > > >> > learn from our mistakes by not changing 'behaviour' anymore. > > >> > > > >> > An alternative syntax that is similar and not ugly would be good > since > > >> > the > > >> > ?: behaviour was broken from the start since you still need to run > > >> > isset() > > >> > before running ?: which was the problem we tried to solve in the > first > > >> > place but it just didn't happen. > > >> > > > >> > I don't like the @ symbol it's too different from what's already > there > > >> > on > > >> > the ternary logic. > > >> > > > >> > Proposal: $foo =3D $arr['value'] ?:: ""; > > >> > > > >> > Thoughts? > > >> > > > >> >> > > >> >> > The would break the assumption that a reference to an > uninitialized > > >> >> > value > > >> >> > would generate a notice, unless explicitly handled in the logic= . > > >> >> > > > >> >> > While I also would like to have something similar to ?: to hand= le > > >> >> > default > > >> >> > values for array keys, etc., this would change a fundamental > > >> >> > assumption > > >> >> > that as been in place for many years now. I'm not sure if that'= s > a > > BC > > >> >> break > > >> >> > that would be acceptable this late. An alternative operator may > be > > >> >> > more > > >> >> > suitable. > > >> >> > > > >> >> > > >> >> What do you think about using: > > >> >> > > >> >> $foo =3D $foo @: 'default'; > > >> >> > > >> >> and possibly > > >> >> > > >> >> $foo @=3D 'default'; > > >> >> > > >> >> > > >> >> > > >> >> > > > >> >> > The change proposed has also been discussed several times since > the > > >> >> > implementation of ?:. See the ifsetor-RFC: > > >> >> > > > >> >> > https://wiki.php.net/rfc/ifsetor > > >> >> > > > >> >> > It also contains links to the discussion around the feature bac= k > > >> >> > then. > > >> >> > > > >> >> > --mats > > >> >> > > > >> >> > > >> > > >> This seems like a cracking time to suggest: > > >> > > >> $x ||=3D $y; > > >> > > >> The difference? Anything left of the ? SHOULD fail if it is not set, > > >> because you are using it in a condition which is dangerous and odd f= or > > >> other types of condition, but adding in this new syntax (similar to > > >> Ruby) would essentially evaluate to this: > > >> > > >> > > >> empty($x) ? $y : $x; > > >> > > >> This gives keeps the behavior of ternary operators working in the sa= me > > >> way, but adds what we want: the ability to nicely set defaults for > > >> variables and array keys that may not be set. > > > > > > > > > To me this reads as $x =3D $x || $y like all the =3D binary opera= tors. > > So it > > > would definitely feel weird to use that syntax for something else... > > > > > >> > > >> > > >> -- > > >> PHP Internals - PHP Runtime Development Mailing List > > >> To unsubscribe, visit: http://www.php.net/unsub.php > > >> > > > > > > > > > > > > -- > > > Etienne Kneuss > > > http://www.colder.ch > > > > It is not $x =3D $x || $y but $x || $x =3D $y. > > > > > > > http://www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-54= 88.html > > > > Ternary is being used by us to set defaults, but it is not the only > > use of ternary, so making it totally quieten potential warnings and > > errors is a weird use of a ternary. It's one of those "we do it this > > way because its all we have" options and going further down the road > > of abusing ternaries for this functionality does not make the > > situation more simple. > > > > Currently: > > > > $foo =3D isset($foo) && $foo ? $foo : 'default'; > > > > Proposed change by OP (with BC break): > > > > $foo =3D $foo ?: 'default'; > > > > New ternary suggested by others, with "error suppressing": > > > > $foo =3D $foo ??: 'default'; > > > > People will try using that with all sorts of other operators involved. > > What happens when you do $foo =3D $foo =3D=3D 'bar' ??: 'default'; It = gets > > complicated. > > > > $foo ||=3D 'default'; > > > > Pretty obvious what is happening. If foo is falsey it will use > > 'default'. We can wrap $foo in isset or empty and make it more > > durable, which certainly would take care of the OP's concerns without > > breaking the way ternaries currently work - which is not actually > > broken just not very handy at setting defaults for non-existent > > variables. > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > --001a11c2fa0674ae1104ec19bf36--