Enumerations

The following enumerations are available globally.

  • The Either type represents values with two possibilities: a value of type Either<A, B> is either left(a) or right(b).

    The Either type is sometimes used to represent a value which is either correct or an error; by convention, the left constructor is used to hold an error value and the right constructor is used to hold a correct value (mnemonic: “right” also means “correct”).

    Examples:

    The type Either<String, Int> is the type of values which can be either a String or an Int. The left constructor can be used only on Strings, and the right constructor can be used only on Ints:

    >>> let s: Either<String, Int> = .left("foo")
    >>> s
    s: Either<String, Int> = left {
        left = "foo"
    }
    >>> let n: Either<String, Int> = .right(3)
    >>> n
    n: Either<String, Int> = right {
        right = 3
    }
    >>> type(of: s)
    Either<String, Int>.Type = Either<String, Int>
    >>> type(of: n)
    Either<String, Int>.Type = Either<String, Int>
    

    The map(_:) from our Functor instance will ignore left values, but will apply the supplied function to values contained in a right:

    >>> let s: Either<String, Int> = .left("foo")
    >>> let n: Either<String, Int> = .right(3)
    >>> s.map { $0 * 2 }
    Either<String, Int> = left {
        left = "foo"
    }
    >>> n.map { $0 * 2 }
    Either<String, Int> = right {
        right = 6
    }
    
    See more

    Declaration

    Swift

    public enum Either<L, R>