CasePaths Documentation

Function extract(case:​from:​)

@available(
  *, deprecated,
  message:
    "Use case path literal syntax (e.g., '/Root.caseName'), or '(/Root.caseName).extract(from:)'"
)
public func extract<Root, Value>(case embed: @escaping (Value) -> Root, from root: Root) -> Value?  

Attempts to extract values associated with a given enum case initializer from a given root enum.

extract(case: Result<Int, Error>.success, from: .success(42))
// 42
extract(case: Result<Int, Error>.success, from: .failure(MyError())
// nil

Parameters

embed @escaping (Value) -> Root

An enum case initializer.

root Root

A root enum value.

Returns

Values if they can be extracted from the given enum case initializer and root enum, otherwise nil.

@available(
  *, deprecated,
  message:
    "Use case path literal syntax (e.g., '/Root.caseName'), or '(/Root.caseName).extract(from:)'"
)
public func extract<Root, Value>(case embed: @escaping (Value) -> Root?, from root: Root?) -> Value? 

Attempts to extract values associated with a given enum case initializer from a given root enum.

extract(case: Result<Int, Error>.success, from: .success(42))
// 42
extract(case: Result<Int, Error>.success, from: .failure(MyError())
// nil

Parameters

embed @escaping (Value) -> Root?

An enum case initializer.

root Root?

A root enum value.

Returns

Values if they can be extracted from the given enum case initializer and root enum, otherwise nil.