Articles and podcasts about Swift development, by John Sundell.

Genius Scan SDK

Presented by the Genius Scan SDK

This article has been archived, as it was published several years ago, so some of its information might now be outdated. For more recent articles, please visit the main article feed.

Specializing generics with type aliases

Published on 17 Jul 2019
Discover page available: Generics

There are so many cool ways that Swift’s type aliases can be used. Here’s an example of specializing a generic using a type alias — which can then be extended and even subclassed!

// Type aliases can be used to easily specialize generics,
// without needing to actually create any additional "real" types.
typealias ProductLoader = ModelLoader<Product>

// Equivalent to 'extension ModelLoader where Model == Product'
extension ProductLoader {
    convenience init(networking: Networking) {
        self.init(networking: networking,
                  endpoint: Endpoint.product)
    }
}

// Equivalent to 'class DiscountedProductLoader: ModelLoader<Product>'
class DiscountedProductLoader: ProductLoader {
    ...
}
Genius Scan SDK

Swift by Sundell is brought to you by the Genius Scan SDK — Add a powerful document scanner to any mobile app, and turn scans into high-quality PDFs with one line of code. Try it today.