Compare
class Comparable =
def (>) (rhs : Self) : bool
def (<) (rhs : Self) : bool
def (>=) (rhs : Self) : bool
def (<=) (rhs : Self) : bool
Class is a contract that that exists during compilation only. Self is the type of an instance on which a method is invoked.
type MutList<T> where T : Comparable
def sort = ...
The sort method can compare list elements due to a restriction on the T parameter.
class Compare =
def compare (other : Self) : Ordering
def (>) (rhs : Self) =
let result = compare rhs
result == Ordering/Greater
def (>=) (rhs : Self) =
let result = compare rhs
result <> Ordering/Less
def (<) (rhs : Self) =
let result = compare rhs
result == Ordering/Less
def (<=) (rhs : Self) =
let result = compare rhs
result <> Ordering/Greater
is Comparable
The Compare class provides comparison operators in exchange for implementing a single compare method.
type (T1, T2) u v where T1 : Comparable
T2 : Comparable
def compare (other : Self) = when
u > other.u -> Ordering/Greater
u < other.u -> Ordering/Less
v > other.v -> Ordering/Greater
v < other.v -> Ordering/Less
else -> Ordering/Equal
is Compare
A tuple of two items is extended, where T1 and T2 are Comparable. u and v are the item names to use in methods. In the end, tuple belongs to both Compare and Comparable classes.