Enumerations
The following enumerations are available globally.
-
The
Either
type represents values with two possibilities: a value of typeEither<A, B>
is eitherleft(a)
orright(b)
.The
Either
type is sometimes used to represent a value which is either correct or an error; by convention, theleft
constructor is used to hold an error value and theright
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 aString
or anInt
. Theleft
constructor can be used only onString
s, and theright
constructor can be used only onInt
s:>>> 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 ourFunctor
instance will ignoreleft
values, but will apply the supplied function to values contained in aright
:
See more>>> 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 }
Declaration
Swift
public enum Either<L, R>