Prior to Swift version 4, we could use generics with subscripts only if the generic type was defined in the containing type; however, we were unable to define a new generic type within the subscript definition. For example, if we had a List type, we could use the generic type defined by the List type within the subscript, as shown in this example:
struct List<T> { /* other implementation code here */ subscript(index: Int) -> T? { return getItemAtIndex(index: index) } }
With Swift version 4 and later, we are able to define generic types within the subscript definition itself. To see how we would do this, let's go ahead and create another very basic generic List type. The following code shows us how to do this:
struct List<T> { private var items = [T]() public mutating func add(item: T) { items...