Weekly Swift articles, podcasts and tips by John Sundell.

Nested generic types

Published on 10 Apr 2018
Basics article available: Generics

Types that are nested in generics automatically inherit their parent's generic types - which is super useful when defining accessory types (for things like states or outcomes).

struct Task<Input, Output> {
    typealias Closure = (Input) throws -> Output

    let closure: Closure
}

extension Task {
    enum Result {
        case success(Output)
        case failure(Error)
    }
}