Arrays
While the original Keeper type illustrates a generic type doesn’t need to store anything or use its type parameter, the most common example of a generic type does both. This is, of course, the Array type.
The need for generic arrays was part of the original motivation to invent generic types. Since so many programs need arrays which are homogeneous, generic arrays make all that code safer. Once the compiler infers (or is told) the type of an array’s elements at one point in the code, it can spot any deviations at other points in the code before the program ever runs.
You’ve been using Array all along, but only with a syntactic sugar: [Element]
instead of Array<Element>. Consider an array declared like so:
let animalAges: [Int] = [2,5,7,9]
This is equivalent to the following:
let animalAges: Array<Int> = [2,5,7,9]
Array<Element> and [Element] are exactly interchangeable. So you could even call an array's default initializer by writing Int instead of Array<Int>().
Since Swift arrays simply allow indexed access to a sequence of elements, they impose no requirements on their Element type. But this isn’t always the case.