Option

public final class Option<A> : OptionOf<A>

Represents optional values. Instances of this type may represent the presence of a value (some) or absence of it (none). This type is isomorphic to native Swift Optional<A> (usually written A?), with the addition of behaving as a Higher Kinded Type.

  • Constructs an instance of Option with presence of a value of the type parameter.

    It equivalent to Option<A>.pure(_:).

    Declaration

    Swift

    public static func some(_ a: A) -> Option<A>

    Parameters

    a

    Value to be wrapped in an Option.

    Return Value

    An option wrapping the value passed as an argument.

  • Constucts an instance of Option with absence of a value.

    It is an alias for Option<A>.empty()

    Declaration

    Swift

    public static func none() -> Option<A>

    Return Value

    An option with no present value

  • Converts a native Swift optional into a value of Option<A>.

    Declaration

    Swift

    public static func fromOptional(_ a: A?) -> Option<A>

    Parameters

    a

    Optional value to be converted.

    Return Value

    An Option with the same structure as the argument.

  • Safe downcasting.

    Declaration

    Swift

    public static func fix(_ fa: OptionOf<A>) -> Option<A>

    Parameters

    fa

    Option in higher-kind form.

    Return Value

    Value cast to Option.

  • Checks if this option contains a value

    Declaration

    Swift

    public var isDefined: Bool { get }
  • Applies a function based on the presence or absence of a value.

    Declaration

    Swift

    public func fold<B>(_ ifEmpty: () -> B, _ f: (A) -> B) -> B

    Parameters

    ifEmpty

    A closure that is executed when there is no value in the Option.

    f

    A closure that is executed where there is a value in the Option. In such case, the the inner value is sent as an argument of f.

    Return Value

    Result of applying the corresponding closure based on the value of this object.

  • Applies a predicate to the wrapped value of this option, returning it if the value does not match the predicate, or none otherwise.

    Declaration

    Swift

    public func filterNot(_ predicate: @escaping (A) -> Bool) -> OptionOf<A>

    Parameters

    predicate

    Boolean predicate to test the wrapped value.

    Return Value

    This value if it does not match the predicate, or none otherwise.

  • Obtains the wrapped value, or a default value if absent.

    Declaration

    Swift

    public func getOrElse(_ defaultValue: A) -> A

    Parameters

    defaultValue

    Value to be returned if this option is empty.

    Return Value

    The value wrapped in this Option, if present, or the value provided as an argument, otherwise.

  • Obtains the wrapped value, or a default value if absent.

    Declaration

    Swift

    public func getOrElse(_ defaultValue: () -> A) -> A

    Parameters

    defaultValue

    Closure to be evaluated if there is no wrapped value in this option.

    Return Value

    The value wrapped in this Option, if present, or the result of running the closure provided as an argument, otherwise.

  • Obtains this option, or a default value if this option is empty.

    Declaration

    Swift

    public func orElse(_ defaultValue: Option<A>) -> Option<A>

    Parameters

    defaultValue

    Default option value to be returned if this option is empty.

    Return Value

    This option, if has a present value, or the value provided as an argument, otherwise.

  • Obtains this option, or a default value if this option is empty.

    Declaration

    Swift

    public func orElse(_ defaultValue: () -> Option<A>) -> Option<A>

    Parameters

    defaultValue

    Closure returning an option for the empty case.

    Return Value

    This option, if has a present value, or the result of running the closure provided as an argument, otherwise.

  • Converts this option into a native Swift optional A?.

    Declaration

    Swift

    public func toOptional() -> A?

    Return Value

    A Swift Optional with the same structure as this value.

  • Converts this option into an array.

    Declaration

    Swift

    public func toArray() -> [A]

    Return Value

    An empty array if this value is absent, or a singleton array, if present.

  • Converts this option into a native Swift optional A?

    Declaration

    Swift

    public var orNil: A? { get }
  • Provides an Iso to go from/to this type to its Kind version.

    Declaration

    Swift

    static var fixIso: Iso<Option<A>, OptionOf<A>> { get }
  • Provides a Fold based on the Foldable instance of this type.

    Declaration

    Swift

    static var fold: Fold<Option<A>, A> { get }
  • Provides a Traversal based on the Traverse instance of this type.

    Declaration

    Swift

    static var traversal: Traversal<Option<A>, A> { get }
  • Undocumented

    Declaration

    Swift

    public typealias EachFoci = A
  • Declaration

    Swift

    public static var each: Traversal<Option<A>, A> { get }
  • Provides a polymorphic Iso between Option and Swift Optional.

    Declaration

    Swift

    static func toPOption<B>() -> PIso<Option<A>, Option<B>, A?, B?>

    Return Value

    A polymorphic Iso between Option and Swift Optional.

  • Provides an Iso between Option and Swift Optional.

    Declaration

    Swift

    static var toOption: Iso<Option<A>, A?> { get }
  • Provides a polymorphic prism focused on the some side of an Option.

    Declaration

    Swift

    static func pSomePrism<B>() -> PPrism<Option<A>, Option<B>, A, B>

    Return Value

    A polymorphic prism focused on the some side of an Option.

  • Provides a prism focused on the some side of an Option.

    Declaration

    Swift

    static var somePrism: Prism<Option<A>, A> { get }
  • Provides a prism focused on the none side of an Option.

    Declaration

    Swift

    static var nonePrism: Prism<Option<A>, ()> { get }
  • Provides a polymorphic Iso between Option and Either.

    Declaration

    Swift

    static func toPEither<B>() -> PIso<Option<A>, Option<B>, Either<(), A>, Either<(), B>>

    Return Value

    A polymorphic Iso between Option and Either.

  • Provides a polymorphic Iso between Option and Either.

    Declaration

    Swift

    static var toEither: Iso<Option<A>, Either<(), A>> { get }
  • Declaration

    Swift

    public var description: String { get }
  • Declaration

    Swift

    public func combine(_ other: Option<A>) -> Option<A>
  • Declaration

    Swift

    public static func empty() -> Option<A>