rfc:combined-comparison-operator
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:combined-comparison-operator [2015/01/19 14:12] – ajf | rfc:combined-comparison-operator [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Combined Comparison (Spaceship) Operator ====== | ====== PHP RFC: Combined Comparison (Spaceship) Operator ====== | ||
- | * Version: 0.2 | + | * Version: 0.2.1 |
* Date: 2014-02-12 (original), 2015-01-19 (v0.2) | * Date: 2014-02-12 (original), 2015-01-19 (v0.2) | ||
- | * Authors: Davey Shafik < | + | * Authors: Davey Shafik < |
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This RFC adds a new operator for combined comparison. Similar to '' | + | This RFC adds a new operator for combined comparison. Similar to '' |
===== Proposal ===== | ===== Proposal ===== | ||
- | Add a new operator '' | + | Add a new operator '' |
- | This operator | + | This [[https:// |
For [[http:// | For [[http:// | ||
- | It makes writing sorting callbacks for use with '' | + | It is implemented by using the result of the existing internal |
- | <code php> | + | ^ operator |
- | function sort_func($a, $b) { | + | | '' |
- | | + | | '' |
- | } | + | | '' |
- | </code> | + | | '' |
- | + | | '' | |
- | Hopefully, with this operator, they can now write proper sort functions. | + | | '' |
- | It uses the existing '' | + | Here are some examples of its behaviour: |
<code php> | <code php> | ||
Line 70: | Line 70: | ||
echo $a <=> $b; // 1 | echo $a <=> $b; // 1 | ||
- | // only values are compared | ||
$a = (object) [" | $a = (object) [" | ||
$b = (object) [" | $b = (object) [" | ||
Line 91: | Line 90: | ||
}); | }); | ||
</ | </ | ||
+ | |||
+ | ===== Usefulness ===== | ||
+ | |||
+ | It makes writing ordering callbacks for use with '' | ||
+ | |||
+ | <code php> | ||
+ | function order_func($a, | ||
+ | return $a >= $b; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | When users do write correct ordering functions, they have to be quite verbose: | ||
+ | |||
+ | <code php> | ||
+ | function order_func($a, | ||
+ | return ($a < $b) ? -1 : (($a > $b) ? 1 : 0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This becomes particularly bad when sorting by multiple columns lexicographically. | ||
+ | |||
+ | With this operator, you can easily write proper ordering functions, like this one: | ||
+ | |||
+ | <code php> | ||
+ | function order_func($a, | ||
+ | return $a <=> $b; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Sorting by multiple columns is simpler now, too: | ||
+ | |||
+ | <code php> | ||
+ | function order_func($a, | ||
+ | return [$a->x, $a->y, $a->foo] <=> [$b->x, $b->y, $b-> | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Or: | ||
+ | |||
+ | <code php> | ||
+ | function order_func($a, | ||
+ | return ($a->$x <=> $b->x) ?: ($a->y <=> $b->y) ?: ($a->foo <=> $b-> | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | It is also useful in some other contexts. | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 106: | Line 151: | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
- | All existing comparison operators, particularly '' | + | All existing comparison operators are unaffected by this addition. |
===== Future Scope ===== | ===== Future Scope ===== | ||
Line 112: | Line 157: | ||
None. | None. | ||
- | ===== Proposed Voting Choices | + | ===== Vote ===== |
+ | |||
+ | Voting started on 2015-02-02 and will to end 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=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
- | 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 ===== | ||
Line 126: | Line 177: | ||
===== Changelog ===== | ===== Changelog ===== | ||
+ | * v0.2.1 - Clarity on type-juggling behaviour and relation to other comparison operators | ||
* v0.2 - Updated, retargeted to PHP 7 by Andrea | * v0.2 - Updated, retargeted to PHP 7 by Andrea | ||
* v0.1 - Initial version by Shafik | * v0.1 - Initial version by Shafik |
rfc/combined-comparison-operator.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1