concept associated type in category swift

appears as: ssociated types, Associated types, associated types, n associated type, associated type, An associated type
Swift in Depth

This is an excerpt from Manning's book Swift in Depth.

Once Swift has you hooked, you’ll likely hit speed bumps in the shape of generics and associated types, and something as “simple” as handling strings may cause more trouble than you might expect (see figure 1.2).

8.2.4. Modeling a protocol with associated types

You’ve seen two alternatives that were not the real solutions to your problem. You’ll create a viable solution to the problem. You’re going to follow the compiler’s advice and use associated types. You can rewrite Worker and use the associatedtype keyword, where you declare both the Input and Output generics as associated types.

Listing 8.15. Worker with associated types
protocol Worker {                         #1
    associatedtype Input                  #2
    associatedtype Output                 #2
 
    @discardableResult
    func start(input: Input) -> Output    #3
}

Now the Input and Output generics are declared as associated types. Associated types are similar to generics, but they are defined inside a protocol. Notice how Worker does not have the <Input, Output> notation. With Worker in place, you can start to conform to it for both MailJob and FileRemover.

8.3. Passing protocols with associated types

Let’s see the ways you can pass a protocol with associated types around. You’ll use the Worker protocol from the last section with two associated types named Input and Output.

  • Associated types are generics that are tied to a protocol.
  • Protocols with associated types allow a concrete type to define the associated type. Each concrete type can specialize an associated type to a different type.
  • Protocols with Self requirements are a unique flavor of associated types referencing the current type.
  • Protocols with associated types or Self requirements force you to reason about types at compile time.
  • You introduce a new Song type, but instead of playing a file at a URL, it uses an AudioFile type. How would you deal with this? See if you can make the protocol reflect this. Answer: You introduce an associated type, such as Media. The contents property is now of type Media, which resolves to something different for each implementation:
    protocol Playable {
        associatedtype Media
        var contents: Media { get }
        func play()
    }
    
    final class Movie: Playable {
        let contents: URL
    
        init(contents: URL) {
            self.contents = contents
        }
    
        func play() { print("Playing video at \(contents)") }
    }
    
    struct AudioFile {}
    final class Song: Playable {
        let contents: AudioFile
    
        init(contents: AudioFile) {
            self.contents = contents
        }
    
        func play() { print("Playing song") }
    }
  • sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage
    test yourself with a liveTest