rfc:comparable
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:comparable [2010/09/30 13:40] – Bah. Add a title. aharvey | rfc:comparable [2016/02/02 02:58] – add problem jhdxr | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Request for Comments: Comparable ====== | ====== Request for Comments: Comparable ====== | ||
- | * Version: | + | * Version: |
- | * Date: 2010-09-30 | + | * Date: 2010-09-30, updated 2015-02-19 |
* Author: Adam Harvey < | * Author: Adam Harvey < | ||
* Status: Under Discussion | * Status: Under Discussion | ||
Line 7: | Line 7: | ||
- | This RFC offers a Comparable interface which can be used to implement userspace | + | This RFC offers a Comparable interface which can be used to implement userspace |
===== Introduction ===== | ===== Introduction ===== | ||
- | Many other languages offer the ability to provide a method on a class which will be used when instances of that class are compared via comparison or equality operators. For example, Java provides the [[http://download-llnw.oracle.com/ | + | Many other languages offer the ability to provide a method on a class which will be used when instances of that class are compared via comparison or equality operators. For example, Java provides the [[http://docs.oracle.com/ |
+ | |||
+ | The need for this has only increased as time has gone past, and particularly with the recent implementation of the spaceship operator. | ||
===== Interface ===== | ===== Interface ===== | ||
Line 20: | Line 22: | ||
<?php | <?php | ||
interface Comparable { | interface Comparable { | ||
- | public function compareTo($o); | + | public function compareTo($other); |
} | } | ||
?> | ?> | ||
</ | </ | ||
+ | |||
+ | This interface intentionally allows variables of any type to be passed to compareTo(): | ||
===== Sample Code ===== | ===== Sample Code ===== | ||
Line 71: | Line 75: | ||
===== Implementation ===== | ===== Implementation ===== | ||
- | The patch below implements the Comparable interface | + | The PR linked |
+ | |||
+ | The only change | ||
+ | |||
+ | ===== FAQ ===== | ||
+ | |||
+ | === Is only $a checked for Comparable or also $b? === | ||
+ | |||
+ | Both $a and $b are checked. | ||
+ | |||
+ | === How it is ensured that if $a < $b then $b > $a? === | ||
+ | |||
+ | That's up to the userspace developer to get right when they' | ||
+ | |||
+ | === Would sorting work with it? === | ||
+ | |||
+ | Yes. | ||
+ | |||
+ | === If both $a and $b are objects with different compare functions, how it is determined whose function is used? Note that operators like == are assumed to be commutative, and less/more operators are assumed to be commutative in pairs, like above. === | ||
+ | |||
+ | The left operand wins, so $a. | ||
+ | |||
+ | === What if the objects are not of the same class? What if one is of a subclass of the other? Is equality not symmetric? === | ||
+ | |||
+ | Equality is symmetric | ||
+ | |||
+ | === How does this interact with ' | ||
+ | |||
+ | The tests for whether the operands implement Comparable occur before any potential calls to get and cast_object. | ||
+ | |||
+ | === How does this interact with the compare_objects handler? === | ||
+ | |||
+ | This will take precedence over a compare_objects handler. This is consistent with the current | ||
+ | |||
+ | In practice, I think it's unlikely that an internal class (or a class extending such an internal class) with a compare_objects handler would also implement Comparable. | ||
+ | |||
+ | ===== Concerns ===== | ||
+ | |||
+ | I'll attempt to summarise the key arguments against this below. Please feel free to edit this if you don't feel your position is accurately represented; | ||
+ | |||
+ | * It's operator overloading; | ||
+ | * Comparison | ||
+ | * Comparison of objects with objects of a different class may be confusing or ill-defined. | ||
+ | * Why we use interface instead of magic method. Comparable is not reserved currently, but methods started with __ are always reserved. | ||
+ | |||
+ | ===== Alternative Approaches ===== | ||
+ | |||
+ | === Traits === | ||
+ | |||
+ | Stas suggests the following as an alternative to overloading the operator: | ||
+ | |||
+ | >As a side note, if we have traits we might instead think of having Comparable trait or interface or both, which would declare having compareTo() standard feature (as Java does) without | ||
+ | |||
+ | ===== Patches ===== | ||
+ | |||
+ | * PR against master: [[https:// | ||
+ | * Obsolete v1.1 patch against 5.4: [[http:// | ||
+ | * Obsolete v1.0 patch against 5.4: [[http:// | ||
- | ===== Patch ===== | + | ===== Changelog |
- | [[http://www.adamharvey.name/ | + | * 2015-02-19: Reanimated RFC; developed new patch that uses the compare handler instead of heavily changing compare_function(). |
+ | * 2010-10-01: Revised patch to remove SPL dependency; added a few questions and answers from the mailing list and a list of concerns raised. | ||
+ | * 2010-09-30: Initial proposal. |
rfc/comparable.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1