December 2018
Intermediate to advanced
414 pages
10h 19m
English
You can also use associated types in your protocols. These associated types let you define protocols that are generics, like this: RunnableWithResult. We can implement a bunch of logic and code around the run() method, without actually knowing anything about the return types. We'll encounter this construction many times in this book, so it's important that you're comfortable with associate types:
protocol RunnableWithResult { associatedtype ResultType func run() -> ResultType}struct RunnersWithResult<T>: RunnableWithResult where T: RunnableWithResult { let runnables: [T] func run() -> [T.ResultType] { return runnables.map { $0.run() } }}
Like with generic types, you can't mix and match heterogeneous ...
Read now
Unlock full access