Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision |
rfc:combined-comparison-operator [2015/01/22 23:57] – ajf | rfc:combined-comparison-operator [2015/02/02 12:05] – Vote ajf |
---|
* Date: 2014-02-12 (original), 2015-01-19 (v0.2) | * Date: 2014-02-12 (original), 2015-01-19 (v0.2) |
* Authors: Davey Shafik <davey@php.net>, Andrea Faulds <ajf@ajf.me> | * Authors: Davey Shafik <davey@php.net>, Andrea Faulds <ajf@ajf.me> |
* Status: Under Discussion | * Status: Voting |
* First Published at: http://wiki.php.net/rfc/combined-comparison-operator | * First Published at: http://wiki.php.net/rfc/combined-comparison-operator |
| |
Add a new operator ''(expr) %%<=>%% (expr)'', it returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater. It uses exactly the same comparison rules as used by our existing comparison operators: ''%%<, <=, ==, >=%%'' and ''>''. (See the manual for [[http://php.net/manual/en/language.operators.comparison.php|details]]) | Add a new operator ''(expr) %%<=>%% (expr)'', it returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater. It uses exactly the same comparison rules as used by our existing comparison operators: ''%%<, <=, ==, >=%%'' and ''>''. (See the manual for [[http://php.net/manual/en/language.operators.comparison.php|details]]) |
| |
This operator (''%%<=>%%'') is often called the spaceship operator, and works on all standard PHP values. It exists in other languages: Perl, Groovy and Ruby. | This [[https://en.wikipedia.org/wiki/Three-way_comparison|"three-way comparison operator"]], also known as the "spaceship operator" (a common name in other languages), works on all standard PHP values. It exists in other languages: [[http://perldoc.perl.org/perlop.html#Equality-Operators|Perl]], [[http://ruby-doc.org/core-1.9.3/Comparable.html|Ruby]], and Groovy. |
| |
For [[http://perldoc.perl.org/perlop.html#Operator-Precedence-and-Associativity|consistency with Perl]], it has the same precedence as ''=='' and ''!=''. | For [[http://perldoc.perl.org/perlop.html#Operator-Precedence-and-Associativity|consistency with Perl]], it has the same precedence as ''=='' and ''!=''. |
It is implemented by using the result of the existing internal ''compare_function'' that underlies the other comparison operators. The existing comparison operators could be considered mere shorthands for ''%%<=>%%'': | It is implemented by using the result of the existing internal ''compare_function'' that underlies the other comparison operators. The existing comparison operators could be considered mere shorthands for ''%%<=>%%'': |
| |
* ''$a < $b'' is equivalent to ''%%($a <=> $b) === -1%%'' | ^ operator ^ ''%%<=>%%'' equivalent ^ |
* ''$a > $b'' is equivalent to ''%%($a <=> $b) === 1%%'' | | ''$a < $b'' | ''%%($a <=> $b) === -1%%'' | |
* ''$a == $b'' is equivalent to ''%%($a <=> $b) === 0%%'' | | ''%%$a <= $b%%'' | ''%%($a <=> $b) === -1 || ($a <=> $b) === 0%%'' | |
* (and so on) | | ''$a == $b'' | ''%%($a <=> $b) === 0%%'' | |
| | ''$a != $b'' | ''%%($a <=> $b) !== 0%%'' | |
| | ''%%$a >= $b%%'' | ''%%($a <=> $b) === 1 || ($a <=> $b) === 0%%'' | |
| | ''$a > $b'' | ''%%($a <=> $b) === 1%%'' | |
| |
Here are some examples of its behaviour: | Here are some examples of its behaviour: |
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== |
| |
All existing comparison operators, particularly ''%%<=%%'' are unaffected by this addition. | All existing comparison operators are unaffected by this addition. |
| |
===== Future Scope ===== | ===== Future Scope ===== |
None. | None. |
| |
===== Proposed Voting Choices ===== | ===== Vote ===== |
| |
| Voting started on 2015-02-02 and ends on 2015-02-16. As this adds to the PHP language (and hence affects the PHP language specification) a 2/3 majority is required for acceptance. It is a Yes/No vote to accepting the RFC and merging the patch. |
| |
| <doodle title="Accept the Combined Comparison (Spaceship) Operator RFC and merge patch into master?" auth="ajf" voteType="single" closed="false"> |
| * Yes |
| * No |
| </doodle> |
| |
A two third majority is required for acceptance, as it is a language change. It will be a Yes/No vote to accepting the RFC and merging. | |
| |
===== Patches and Tests ===== | ===== Patches and Tests ===== |