Comparable

public protocol Comparable

Protocol 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

Returns 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

Methods

public func equal(to: Self) -> Bool

Bridges Equal.equal(to:) to Equatable.isEqual(to:).

func isEqual(to: Self) -> Bool

Returns 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

Default !=: delegates to == so there's a single source of truth.

ImplementsLess

Associated Types

type Output = Bool
type Output = Bool
type Output = Bool
type Output = Bool

Methods

public func lessThan(Self) -> Bool

< derived from compare.

ImplementsLessOrEqual

Associated Types

type Output

Methods

public func lessThanOrEqual(Self) -> Bool

<= derived from compare.

ImplementsGreater

Associated Types

type Output

Methods

public func greaterThan(Self) -> Bool

> derived from compare.

ImplementsGreaterOrEqual

Associated Types

type Output

Methods

public func greaterThanOrEqual(Self) -> Bool

>= derived from compare.

ImplementsRangeMatchable

Methods

public func isAtLeast(Self) -> Bool

start.. lower-bound check, derived from compare.

public func isAtMost(Self) -> Bool

..=end upper-bound check, derived from compare.

public func isBelow(Self) -> Bool

..<end upper-bound check, derived from compare.

Defined in lang/std/core/protocols.ks