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 DateTimes in different timezones may represent the same instant in time. However, two DateTimes 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
Mixin applications

Properties

hashCode int
The hash code for this object.
no setteroverride
hashValue int
This object's hash.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

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 than other.