Structure
CasePath
public struct CasePath<Root, Value>
A path that supports embedding a value in a root and attempting to extract a root's embedded value.
This type defines key path-like semantics for enum cases.
Relationships
Conforms To
CustomStringConvertible
Initializers
init(embed:extract:)
public init(
embed: @escaping (Value) -> Root,
extract: @escaping (Root) -> Value?
)
Creates a case path with a pair of functions.
Parameters
Name | Type | Description |
---|---|---|
embed | @escaping (Value) -> Root |
A function that always succeeds in embedding a value in a root. |
extract | @escaping (Root) -> Value? |
A function that can optionally fail in extracting a value from a root. |
init(_:)
public init(_ embed: @escaping (Value) -> Root)
Returns a case path for the given embed function.
Parameters
Name | Type | Description |
---|---|---|
embed | @escaping (Value) -> Root |
An embed function. |
Returns
A case path.
Properties
description
public var description: String
Methods
embed(_:)
public func embed(_ value: Value) -> Root
Returns a root by embedding a value.
Parameters
Name | Type | Description |
---|---|---|
value | Value |
A value to embed. |
Returns
A root that embeds value
.
extract(from:)
public func extract(from root: Root) -> Value?
Attempts to extract a value from a root.
Parameters
Name | Type | Description |
---|---|---|
root | Root |
A root to extract from. |
Returns
A value if it can be extracted from the given root, otherwise nil
.
modify(_:_:)
public func modify<Result>(
_ root: inout Root,
_ body: (inout Value) throws -> Result
) throws -> Result
Attempts to modify a value in a root.
Parameters
Name | Type | Description |
---|---|---|
root | inout Root |
A root to modify if the case path matches. |
body | (inout Value) throws -> Result |
A closure that can mutate the case's associated value. If the closure throws, the root will be left unmodified. |
Returns
The return value, if any, of the body closure.
appending(path:)
public func appending<AppendedValue>(path: CasePath<Value, AppendedValue>) -> CasePath<
Root, AppendedValue
>
Returns a new case path created by appending the given case path to this one.
Use this method to extend this case path to the value type of another case path.
Parameters
Name | Type | Description |
---|---|---|
path | CasePath<Value, AppendedValue> |
The case path to append. |
Returns
A case path from the root of this case path to the value type of path
.
`case`(_:)
@available(*, deprecated, message: "Use case path literal syntax (e.g., '/Root.caseName')")
public static func `case`(_ embed: @escaping (Value) -> Root) -> CasePath
Returns a case path that extracts values associated with a given enum case initializer.
Parameters
Name | Type | Description |
---|---|---|
embed | @escaping (Value) -> Root |
An enum case initializer. |
Returns
A case path that extracts associated values from enum cases.
..(lhs:rhs:)
public static func .. <AppendedValue>(
lhs: CasePath,
rhs: CasePath<Value, AppendedValue>
) -> CasePath<Root, AppendedValue>
Returns a new case path created by appending the given case path to this one.
The operator version of appending(path:)
. Use this method to extend this case path to the
value type of another case path.
Parameters
Name | Type | Description |
---|---|---|
lhs | CasePath |
A case path from a root to a value. |
rhs | CasePath<Value, AppendedValue> |
A case path from the first case path's value to some other appended value. |
Returns
A new case path from the first case path's root to the second case path's value.
..(lhs:rhs:)
public static func .. <AppendedValue>(
lhs: CasePath,
rhs: @escaping (AppendedValue) -> Value
) -> CasePath<Root, AppendedValue>
Returns a new case path created by appending the given embed function.
Parameters
Name | Type | Description |
---|---|---|
lhs | CasePath |
A case path from a root to a value. |
rhs | @escaping (AppendedValue) -> Value |
An embed function from an appended value. |
Returns
A new case path from the first case path's root to the second embed function's value.