[RFC] Deprecate left-associative ternary operator

  105163
April 9, 2019 09:54 nikita.ppv@gmail.com (Nikita Popov)
Hi internals,

Inspired by Bob's recent RFC for concat precedence, I'd like to propose a
deprecation and removal of the left-associative behavior of ternaries.
Instead, explicit parentheses should be used:

https://wiki.php.net/rfc/ternary_associativity

This RFC makes nested ternaries without disambiguating parentheses an error
in PHP 8 -- we might want to consider making them right-associative
instead, which is both useful and matches the behavior of other languages.

Regards,
Nikita
  105164
April 9, 2019 10:19 bishop@php.net (Bishop Bettini)
On Tue, Apr 9, 2019 at 5:54 AM Nikita Popov ppv@gmail.com> wrote:

> > Inspired by Bob's recent RFC for concat precedence, I'd like to propose a > deprecation and removal of the left-associative behavior of ternaries. > Instead, explicit parentheses should be used: > > https://wiki.php.net/rfc/ternary_associativity > > This RFC makes nested ternaries without disambiguating parentheses an error > in PHP 8 -- we might want to consider making them right-associative > instead, which is both useful and matches the behavior of other languages. >
I'd support mandatory disambiguation, though I feel we should go all in and introduce a ternary with right associativity. See also Bug 61915[1], which suggests leaving ? left and introducing ?? for right. Personally I don't see any benefit to keeping a ternary with left associativity. [1]: https://bugs.php.net/bug.php?id=61915
  105166
April 9, 2019 10:24 rowan.collins@gmail.com (Rowan Collins)
On Tue, 9 Apr 2019 at 10:54, Nikita Popov ppv@gmail.com> wrote:

> Hi internals, > > Inspired by Bob's recent RFC for concat precedence, I'd like to propose a > deprecation and removal of the left-associative behavior of ternaries. > Instead, explicit parentheses should be used: > > https://wiki.php.net/rfc/ternary_associativity >
Thanks for putting this forward, it removes one more WTF from the language. I think non-associativity is safest, because even if it's rarely desirable, code may be out there that works accidentally with the current situation. There's also the risk that someone would get used to it working one way in PHP 8, and accidentally use it in code that's still deployed on PHP 7. In both cases, it's better to generate an error and force users to be explicit than introduce subtle bugs. We can then look at making it right-associative in PHP 9, because anybody that needs to support 7, 8 and 9 can be explicit, and hopefully not many people will be jumping directly between 7 and 9. Regards, -- Rowan Collins [IMSoP]
  105172
April 9, 2019 11:36 gadelat@gmail.com (Gabriel O)
Hello, I am interested to know if this affects behaviour of following code:

putenv($this->colSize ? 'COLUMNS' : 'COLUMNS='.$this->colSize);

> On 9. Apr 2019, at 11:54, Nikita Popov ppv@gmail.com> wrote: > > Hi internals, > > Inspired by Bob's recent RFC for concat precedence, I'd like to propose a > deprecation and removal of the left-associative behavior of ternaries. > Instead, explicit parentheses should be used: > > https://wiki.php.net/rfc/ternary_associativity > > This RFC makes nested ternaries without disambiguating parentheses an error > in PHP 8 -- we might want to consider making them right-associative > instead, which is both useful and matches the behavior of other languages. > > Regards, > Nikita
  105219
April 10, 2019 19:21 smalyshev@gmail.com (Stanislav Malyshev)
Hi!

> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a > deprecation and removal of the left-associative behavior of ternaries. > Instead, explicit parentheses should be used: > > https://wiki.php.net/rfc/ternary_associativity
Please, let's not mess with language syntax and require people to rewrite their code just because suddenly after 20 years of existence in the language we feel maybe the precedence should be different. I literally can't think of a single case where it would improve anyone's life in any substantial way. If you wanted to use parentheses, you can do it now - you can even have it enforced by code analyzer if you wish so, in your private code base. But I can easily imagine dealing with tons of deprecation warnings (which usually break CI unit tests and wreak general havoc) or even worse, useless errors, in existing code, which would become broken for no reason but... honestly, I don't even know how to finish this phrase, I can't even think of a single reason I could explain to anybody why their code would no longer work in 8.0. I understand 8.0 is a good target for breaking things. But that doesn't mean we have to do it as much as possible. And this absolutely doesn't mean we need to get 7.4 to break things (and yes, deprecation warning is breaking things, in most cases). -- Stas Malyshev smalyshev@gmail.com
  105221
April 10, 2019 20:33 levim@php.net (Levi Morrison)
On Wed, Apr 10, 2019 at 1:22 PM Stanislav Malyshev <smalyshev@gmail.com> wrote:
> > Hi! > > > Inspired by Bob's recent RFC for concat precedence, I'd like to propose a > > deprecation and removal of the left-associative behavior of ternaries. > > Instead, explicit parentheses should be used: > > > > https://wiki.php.net/rfc/ternary_associativity > > Please, let's not mess with language syntax and require people to > rewrite their code just because suddenly after 20 years of existence in > the language we feel maybe the precedence should be different. I > literally can't think of a single case where it would improve anyone's > life in any substantial way. If you wanted to use parentheses, you can > do it now - you can even have it enforced by code analyzer if you wish > so, in your private code base. But I can easily imagine dealing with > tons of deprecation warnings (which usually break CI unit tests and > wreak general havoc) or even worse, useless errors, in existing code, > which would become broken for no reason but... honestly, I don't even > know how to finish this phrase, I can't even think of a single reason I > could explain to anybody why their code would no longer work in 8.0. > > I understand 8.0 is a good target for breaking things. But that doesn't > mean we have to do it as much as possible. > > And this absolutely doesn't mean we need to get 7.4 to break things (and > yes, deprecation warning is breaking things, in most cases). > -- > Stas Malyshev > smalyshev@gmail.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php
We do not consider new warnings, including deprecation notices, as breaking changes. We know that due to error handlers it can have an effect, but it's just part of the process. I disagree with Stas on the value. The precedence is wrong, and I know of a few codebases which had bugs due to this. In every case, the fix was to avoid it altogether. I agree with statement from the RFC:
> As left-associative ternaries are almost certainly bugs, the impact of this change should be minimal.
Thanks, Nikita, on putting this together. It's definitely not a "fun" RFC.
  105222
April 10, 2019 20:42 claude.pache@gmail.com (Claude Pache)
> Le 10 avr. 2019 à 21:21, Stanislav Malyshev <smalyshev@gmail.com> a écrit : > > Hi! > >> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a >> deprecation and removal of the left-associative behavior of ternaries. >> Instead, explicit parentheses should be used: >> >> https://wiki.php.net/rfc/ternary_associativity > > Please, let's not mess with language syntax and require people to > rewrite their code just because suddenly after 20 years of existence in > the language we feel maybe the precedence should be different. I > literally can't think of a single case where it would improve anyone's > life in any substantial way.
I bet that more than one developer has once written , and later has wasted a nontrivial amount of time in order to understand why they got some wrong result. So, definitely, making that trap a syntax error is a real tangible improvement of the language.
> If you wanted to use parentheses, you can > do it now - you can even have it enforced by code analyzer if you wish > so, in your private code base.
A language that requires their users to use a code analyser (or to read the manual thoroughly before starting to code) in order to keep their sanity, well, ... has a problem.
> But I can easily imagine dealing with > tons of deprecation warnings (which usually break CI unit tests and > wreak general havoc) or even worse, useless errors, in existing code, > which would become broken for no reason
As I can’t imagine a case where someone would have taken advantage of the current left-associativity semantics, I can’t imagine either someone dealing with “a ton of deprecation warnings” because of that. But TBH, my imagination is sometimes very selective. However, what I can imagine, is that hidden bugs will be revealed as a side-effect of those deprecation warnings, and time will be taken to understand and correct them.. —Claude
> but... honestly, I don't even > know how to finish this phrase, I can't even think of a single reason I > could explain to anybody why their code would no longer work in 8.0. > > I understand 8.0 is a good target for breaking things. But that doesn't > mean we have to do it as much as possible. > > And this absolutely doesn't mean we need to get 7.4 to break things (and > yes, deprecation warning is breaking things, in most cases). > -- > Stas Malyshev > smalyshev@gmail.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
  105220
April 10, 2019 20:10 mike.php.net@gmail.com (Michael Wallner)
Nikita Popov ppv@gmail.com> schrieb am Di., 9. Apr. 2019, 11:54:

> Hi internals, > > Inspired by Bob's recent RFC for concat precedence, I'd like to propose a > deprecation and removal of the left-associative behavior of ternaries. > Instead, explicit parentheses should be used: > > https://wiki.php.net/rfc/ternary_associativity > > This RFC makes nested ternaries without disambiguating parentheses an error > in PHP 8 -- we might want to consider making them right-associative > instead, which is both useful and matches the behavior of other languages. > > Regards, > Nikita >
Simple, +1. Regards, Mike
  105223
April 10, 2019 20:44 bjorn.x.larsson@telia.com (=?UTF-8?Q?Bj=c3=b6rn_Larsson?=)
Den 2019-04-09 kl. 11:54, skrev Nikita Popov:
> Hi internals, > > Inspired by Bob's recent RFC for concat precedence, I'd like to propose a > deprecation and removal of the left-associative behavior of ternaries. > Instead, explicit parentheses should be used: > > https://wiki.php.net/rfc/ternary_associativity > > This RFC makes nested ternaries without disambiguating parentheses an error > in PHP 8 -- we might want to consider making them right-associative > instead, which is both useful and matches the behavior of other languages. > > Regards, > Nikita Thanks for bringing this up. I recall it was up for discussion in
PHP 7.0, never made it then. Hope it does it now! Should one in the RFC reference this bug report from 2012? - https://bugs.php.net/bug.php?id=61915 r//Björn L
  105269
April 13, 2019 22:12 lauri.kentta@gmail.com (=?UTF-8?Q?Lauri_Kentt=C3=A4?=)
On 2019-04-09 12:54, Nikita Popov wrote:
> Inspired by Bob's recent RFC for concat precedence, I'd like to propose > a > deprecation and removal of the left-associative behavior of ternaries. > Instead, explicit parentheses should be used: ...
What about nesting in the middle part: 1 ? 2 ? 3 : 4 : 5 This is unambiguous and should be allowed IMO. -- Lauri Kenttä
  105307
April 17, 2019 10:21 nikita.ppv@gmail.com (Nikita Popov)
On Sun, Apr 14, 2019 at 12:12 AM Lauri Kenttä kentta@gmail.com>
wrote:

> On 2019-04-09 12:54, Nikita Popov wrote: > > Inspired by Bob's recent RFC for concat precedence, I'd like to propose > > a > > deprecation and removal of the left-associative behavior of ternaries. > > Instead, explicit parentheses should be used: ... > > What about nesting in the middle part: 1 ? 2 ? 3 : 4 : 5 > This is unambiguous and should be allowed IMO. >
This case is not affected, as it's not related to associativity. I've mentioned this in the RFC explicitly now. Nikita
  105328
April 22, 2019 08:09 nikita.ppv@gmail.com (Nikita Popov)
On Tue, Apr 9, 2019 at 11:54 AM Nikita Popov ppv@gmail.com> wrote:

> Hi internals, > The only objection here came from Gabriel, and I don't think we'll come to > an agreement. > Inspired by Bob's recent RFC for concat precedence, I'd like to propose a > deprecation and removal of the left-associative behavior of ternaries. > Instead, explicit parentheses should be used: > > https://wiki.php.net/rfc/ternary_The only objection here came from > Gabriel, and I don't think we'll come to an agreement.associativity > <https://wiki.php.net/rfc/ternary_associativity> > > This RFC makes nested ternaries without disambiguating parentheses an > error in PHP 8 -- we might want to consider making them right-associative > instead, which is both useful and matches the behavior of other languages. > > Regards, > Nikita >
Heads up: Plan to start voting tomorrow. Apart from Bishop, would anyone else prefer to directly go to right associativity in PHP 8 rather than making it an error? I'm rather partial to that myself, maybe it can be included as a voting option... Nikita
  105366
April 24, 2019 09:10 bjorn.x.larsson@telia.com (=?UTF-8?Q?Bj=c3=b6rn_Larsson?=)
Den 2019-04-22 kl. 10:09, skrev Nikita Popov:
> On Tue, Apr 9, 2019 at 11:54 AM Nikita Popov ppv@gmail.com> wrote: > >> Hi internals, >> The only objection here came from Gabriel, and I don't think we'll come to >> an agreement. >> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a >> deprecation and removal of the left-associative behavior of ternaries. >> Instead, explicit parentheses should be used: >> >> https://wiki.php.net/rfc/ternary_The only objection here came from >> Gabriel, and I don't think we'll come to an agreement.associativity >> <https://wiki.php.net/rfc/ternary_associativity> >> >> This RFC makes nested ternaries without disambiguating parentheses an >> error in PHP 8 -- we might want to consider making them right-associative >> instead, which is both useful and matches the behavior of other languages. >> >> Regards, >> Nikita >> > Heads up: Plan to start voting tomorrow. > > Apart from Bishop, would anyone else prefer to directly go to right > associativity in PHP 8 rather than making it an error? I'm rather partial > to that myself, maybe it can be included as a voting option... > > Nikita
Hi, I think one should go for the right associativity. 8.x is the right place to do it, if at all. Doing it directly in 8.0 has the benefit that when upgrading to a major version it justifies an upgrading project for an application and one also might get tool support, e.g. the php7mar tool for 7.0 was quite valuable. Also IDEs like phpstorm has good support. One then has to weigh legacy code versus new code being written with a behaviour different from almost all other languages. I'm not sure if this is an appropriate example to compare with, but I came to think on the PHP 7.0 Uniform Variable Syntax rfc regarding migration effort and BC impact. r//Björn L
  105370
April 24, 2019 13:11 claude.pache@gmail.com (Claude Pache)
> Le 24 avr. 2019 à 11:10, Björn Larsson larsson@telia.com> a écrit : > > Den 2019-04-22 kl. 10:09, skrev Nikita Popov: >> On Tue, Apr 9, 2019 at 11:54 AM Nikita Popov ppv@gmail.com> wrote: >> >>> Hi internals, >>> The only objection here came from Gabriel, and I don't think we'll come to >>> an agreement. >>> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a >>> deprecation and removal of the left-associative behavior of ternaries. >>> Instead, explicit parentheses should be used: >>> >>> https://wiki.php.net/rfc/ternary_The only objection here came from >>> Gabriel, and I don't think we'll come to an agreement.associativity >>> <https://wiki.php.net/rfc/ternary_associativity> >>> >>> This RFC makes nested ternaries without disambiguating parentheses an >>> error in PHP 8 -- we might want to consider making them right-associative >>> instead, which is both useful and matches the behavior of other languages. >>> >>> Regards, >>> Nikita >>> >> Heads up: Plan to start voting tomorrow. >> >> Apart from Bishop, would anyone else prefer to directly go to right >> associativity in PHP 8 rather than making it an error? I'm rather partial >> to that myself, maybe it can be included as a voting option... >> >> Nikita > > Hi, > > I think one should go for the right associativity. 8.x is the right place to > do it, if at all. Doing it directly in 8.0 has the benefit that when upgrading > to a major version it justifies an upgrading project for an application and > one also might get tool support, e.g. the php7mar tool for 7.0 was quite > valuable. Also IDEs like phpstorm has good support. > > One then has to weigh legacy code versus new code being written with a > behaviour different from almost all other languages. I'm not sure if this > is an appropriate example to compare with, but I came to think on the > PHP 7.0 Uniform Variable Syntax rfc regarding migration effort and BC > impact. > > r//Björn L > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
If you go for right-associative straight in PHP 8, you make it mandatory to use a static analysis tool for: * migrating code from one major version to the next; * maintaining a library that aims to run in two major consecutive versions. Although that might be a no-brainer for anyone following internals, I doubt it is the case for anyone writing PHP. With a more step-wise approach (left-associative in PHP 7, non-associative in PHP 8, right-associative in PHP 9 or 10), the situation is much safer and simpler: you can’t even compile the code in at least one of the consecutive major versions you aim to support, and the error message says exactly what and where you have to fix in order to make it working. As says an Italian proverb: chi va piano, va sano e lontano. —Claude
  105330
April 22, 2019 15:28 pollita@php.net (Sara Golemon)
On Tue, Apr 9, 2019 at 4:54 AM Nikita Popov ppv@gmail.com> wrote:

> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a > deprecation and removal of the left-associative behavior of ternaries. > Instead, explicit parentheses should be used: > > https://wiki.php.net/rfc/ternary_associativity > > This RFC makes nested ternaries without disambiguating parentheses an error > in PHP 8 -- we might want to consider making them right-associative > instead, which is both useful and matches the behavior of other languages. > > Sorry, but I'm a "No" on this one.
It'd be swell if PHP's ternary matched other languages, but it doesn't, and we have 20 years of code in the wild which has accepted that fact and moved on. I don't see the benefit in breaking that code. Advise the use of parentheses and move on. -Sara
  105331
April 22, 2019 17:42 smalyshev@gmail.com (Stanislav Malyshev)
Hi!

> It'd be swell if PHP's ternary matched other languages, but it doesn't, and > we have 20 years of code in the wild which has accepted that fact and moved > on. I don't see the benefit in breaking that code. Advise the use of > parentheses and move on.
Exactly. -- Stas Malyshev smalyshev@gmail.com
  105346
April 23, 2019 08:56 nikita.ppv@gmail.com (Nikita Popov)
On Mon, Apr 22, 2019 at 5:28 PM Sara Golemon <pollita@php.net> wrote:

> On Tue, Apr 9, 2019 at 4:54 AM Nikita Popov ppv@gmail.com> wrote: > >> Inspired by Bob's recent RFC for concat precedence, I'd like to propose a >> deprecation and removal of the left-associative behavior of ternaries. >> Instead, explicit parentheses should be used: >> >> https://wiki.php.net/rfc/ternary_associativity >> >> This RFC makes nested ternaries without disambiguating parentheses an >> error >> in PHP 8 -- we might want to consider making them right-associative >> instead, which is both useful and matches the behavior of other languages. >> >> Sorry, but I'm a "No" on this one. > > It'd be swell if PHP's ternary matched other languages, but it doesn't, > and we have 20 years of code in the wild which has accepted that fact and > moved on. I don't see the benefit in breaking that code. Advise the use of > parentheses and move on. >
Can't say I understand your position here. Don't want to change the ternary from left-associative to right-associative? I can understand that: Silent behavior changes are always problematic. This is not what the RFC proposes though. 20 years of code in the wild has not "accepted that fact and moved on". If a left-associative ternary is used, it is almost certainly a bug. If people use this structure by accident (because it is familiar from other programming languages), I'd like them to get an error instead of having to figure out why their obviously correct code is not working or, in the worse case, just leave behind buggy code. Nikita