Either

public enum Either<A, B>
extension Either: CustomStringConvertible
extension Either: Equatable where A: Equatable, B: Equatable
extension Either: Comparable where A: Comparable, B: Comparable
extension Either: Hashable where A: Hashable, B: Hashable

An unbiased tagged union or sum type of exactly two possible cases, .a and .b, having types A and B respectively.

When NOT to use Either: if there are asymmetrical semantics (e.g. A is special in some manner), or when there are better names (i.e. meaning) that can be attached to the cases, a domain-specific enum often results in more maintainable code and easier to use APIs.

When to use Either: good applications of Either come up in generic programming where there are no defined semantics or information that can be gained from naming or biasing one of the two cases.

  • Undocumented

    Declaration

    Swift

    case a(A)
  • Undocumented

    Declaration

    Swift

    case b(B)
  • a

    x iff the value of self is .a(x) for some x; nil otherwise.

    Declaration

    Swift

    public var a: A? { get }
  • b

    x iff the value of self is .b(x) for some x; nil otherwise.

    Declaration

    Swift

    public var b: B? { get }
  • A textual representation of self.

    Declaration

    Swift

    public var description: String { get }

Available where A: Equatable, B: Equatable

  • True iff lhs is equivalent to rhs.

    Declaration

    Swift

    public static func == (lhs: `Self`, rhs: `Self`) -> Bool

Available where A: Comparable, B: Comparable

  • True iff lhs comes before rhs in an ordering where every .a(x)s is ordered before any .b(y), .a(x)s are ordered by increasing x, and .b(y)s are ordered by increasing y.

    Declaration

    Swift

    public static func < (lhs: `Self`, rhs: `Self`) -> Bool

Available where A: Hashable, B: Hashable