rfc:ternary_associativity

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:ternary_associativity [2019/04/09 09:48] – created nikicrfc:ternary_associativity [2019/05/13 15:51] (current) nikic
Line 2: Line 2:
   * Date: 2019-04-09   * Date: 2019-04-09
   * Author: Nikita Popov <nikic@php.net>   * Author: Nikita Popov <nikic@php.net>
-  * Status: Under Discussion+  * Status: Implemented
   * Target Version: PHP 7.4 and PHP 8.0   * Target Version: PHP 7.4 and PHP 8.0
   * Implementation: https://github.com/php/php-src/pull/4017   * Implementation: https://github.com/php/php-src/pull/4017
Line 79: Line 79:
 the same result, even though the left-associative version will arrive at it in a less efficient the same result, even though the left-associative version will arrive at it in a less efficient
 manner. manner.
 +
 +Parentheses are also not required when nesting into the middle operand, as this is always unambiguous and not affected by associativity:
 +
 +<PHP>
 +1 ? 2 ? 3 : 4 : 5 // ok
 +1 ? 2 ?: 3 : 4    // ok
 +</PHP>
  
 The null-coalesce operator ''??'' is already right-associative and not affected by these changes. The null-coalesce operator ''??'' is already right-associative and not affected by these changes.
Line 86: Line 93:
 Code exploiting left-associativity of the ternary operator will become a hard error in PHP 8. As Code exploiting left-associativity of the ternary operator will become a hard error in PHP 8. As
 left-associative ternaries are almost certainly bugs, the impact of this change should be minimal. left-associative ternaries are almost certainly bugs, the impact of this change should be minimal.
 +
 +An analysis of the top 1000 composer packages found 12 instances that will be affected by this RFC. 9 of them are bugs, where a right-associative meaning was intended, but a left-associative one will be used. 3 uses of the form ''$a ? $b : $c ?: $d'' are potentially okay, because the difference between the two interpretations is small and it's not obvious to me which one is actually intended. Detailed analysis results are available at https://gist.github.com/nikic/b6214f87b0e4a7c6fe26919ac849194f.
  
 ===== Future Scope ===== ===== Future Scope =====
  
-We could make the ternary right-associative, either directly in PHP 8 or at a later point in time, +We could make the ternary right-associative in a later release, after it has been an error for a while.
-after it has been an error for a while. Because the current left-associative interpretation is so +
-unlikely to be used intentionally, this is a viable change.+
  
 ===== Vote ===== ===== Vote =====
  
-Straightforward Yes/No vote.+Voting started 2019-04-23 and ends 2019-05-07. 
 + 
 +<doodle title="Deprecate and remove left-associative ternary without explicit parentheses?" auth="nikic" voteType="single" closed="true"> 
 +   Yes 
 +   No 
 +</doodle>
  
rfc/ternary_associativity.1554803311.txt.gz · Last modified: 2019/04/09 09:48 by nikic