December 2018
Intermediate to advanced
414 pages
10h 19m
English
You can also make complex types generic. In our example, we created this wrapper around a list of Runnable, called ManyRunner. The job of a many runner is to run all of the runnables. The ManyRunner is itself Runnable, so we have created a kind of type recursion, as follows:
struct ManyRunner<T>: Runnable where T: Runnable { let runnables: [T] func run() { runnables.forEach { $0.run() } }}
Let's also provide a base object that runs a simple Incrementer. Each time the Incrementer is run, the static count will increment, to keep track of the number of invocations:
struct Incrementer: Runnable { private(set) static var count = 0 func run() { Incrementer.count += 1 }}
When using generics on types, remember that the types have to ...
Read now
Unlock full access