Comparable
public protocol ComparableProtocol for types with a total ordering.
Conformers implement a single compare(other:) -> Ordering; the
blanket extension below derives <, <=, >, >=, and != (the
last shadowing the Equatable default since it can be cheaper via
compare). Comparable extends Equatable, so equal values and a
compare returning .Equal must agree.
Examples
public struct Version: Comparable {
public var major: Int64
public var minor: Int64
public func isEqual(to other: Version) -> Bool {
self.major == other.major and self.minor == other.minor
}
public func compare(other: Version) -> Ordering {
self.major.compare(other.major)
.then(self.minor.compare(other.minor))
}
}Methods
func compare(Self) -> Ordering
func compare(Self) -> OrderingReturns the ordering of self relative to other. Must be a
total order — for any a, b, c exactly one of Less,
Equal, Greater holds, and the order is transitive.
ImplementsEquatable
Associated Types
type Output = Bool
type Output = BoolMethods
public func equal(to: Self) -> Bool
public func equal(to: Self) -> BoolBridges Equal.equal(to:) to Equatable.isEqual(to:).
func isEqual(to: Self) -> Bool
func isEqual(to: Self) -> BoolReturns true iff self and other are considered equal. Should
be reflexive, symmetric, and transitive — Hashable requires equal
values to hash equal, so don't drift from those laws.
public func notEqual(to: Self) -> Bool
public func notEqual(to: Self) -> BoolDefault !=: delegates to == so there's a single source of truth.
ImplementsLess
Associated Types
type Output = Bool
type Output = Booltype Output = Bool
type Output = Booltype Output = Bool
type Output = Booltype Output = Bool
type Output = BoolMethods
public func lessThan(Self) -> Bool
public func lessThan(Self) -> Bool< derived from compare.
ImplementsLessOrEqual
Associated Types
type Output
type OutputMethods
public func lessThanOrEqual(Self) -> Bool
public func lessThanOrEqual(Self) -> Bool<= derived from compare.
ImplementsGreater
Associated Types
type Output
type OutputMethods
public func greaterThan(Self) -> Bool
public func greaterThan(Self) -> Bool> derived from compare.
ImplementsGreaterOrEqual
Associated Types
type Output
type OutputMethods
public func greaterThanOrEqual(Self) -> Bool
public func greaterThanOrEqual(Self) -> Bool>= derived from compare.
ImplementsRangeMatchable
Methods
public func isAtLeast(Self) -> Bool
public func isAtLeast(Self) -> Boolstart.. lower-bound check, derived from compare.
public func isAtMost(Self) -> Bool
public func isAtMost(Self) -> Bool..=end upper-bound check, derived from compare.
public func isBelow(Self) -> Bool
public func isBelow(Self) -> Bool..<end upper-bound check, derived from compare.
Defined in lang/std/core/protocols.ks