Articles, podcasts and news about Swift development, by John Sundell.

Nested generic types

Published on 10 Apr 2018
Discover page 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)
    }
}