Orderable<T extends Orderable<T> > mixin
Simplifies implementation of naturally ordered types.
Orderable implements all comparison operators, including ==, using compareTo. Implementations should have only
one representation for a value. Otherwise == will disagree with the ordering. A counterexample is DateTime.
Several DateTime
s in different timezones may represent the same instant in time. However, two DateTime
s are only
equal if they represent the same instant and share the same timezone.
Implementations should override only the following:
Objects where compareTo is 0 should have the same hashValue. A common mistake is to use an unrelated field when
computing a hashValue
.
Counterexample:
class Foo with Orderable<Foo> {
final String key;
final int value;
Foo(String.key, this.value);
@override
int compareTo(Wrong other) => key.compareTo(other.key);
@override
int get hashValue => Object.hash(key, value);
}
Foo('a', 1) == Foo('a', 2); // true
Foo('a', 1).hashCode == Foo('a', 2).hashCode; // false, violates hash code contract
- Implemented types
-
- Comparable<
T>
- Comparable<
- Mixin applications
Properties
Methods
-
compareTo(
T other) → int -
Compares this object to another object.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator <(
T other) → bool -
Returns true if this is less than
other
. -
operator <=(
T other) → bool -
Returns true if this is equal to or less than
other
. -
operator ==(
Object other) → bool -
The equality operator.
override
-
operator >(
T other) → bool -
Returns true if this is more than
other
. -
operator >=(
T other) → bool -
Returns
true
if this is equal to or more thanother
.