Sequence
For the use of generics in Swift it is important to understand the adoption and inheritance of protocols. Here are the types and protocol(s) that are adopted by Sequence (which is a top-level protocol that adopts no other protocols):struct EmptyGenerator struct EnumerateGenerator struct FilterGenerator struct FilterSequenceView struct GeneratorOf struct GeneratorOfOne struct GeneratorSequence struct IndexingGenerator struct LazySequence struct MapSequenceGenerator struct MapSequenceView struct PermutationGenerator struct RangeGenerator struct SequenceOf struct StrideThrough (note: might adopt Collection in future update instead) struct StrideTo (note: might adopt Collection in future update instead) struct UnsafeArrayGenerator struct Zip2 protocol Collection
Collection
Since the Collection protocol adopts the Sequence protocol, all types and protocols that adopt Collection also inherit the Sequence protocol. And the types and protocol(s) that adopt Collection are:struct BidirectionalReverseView struct CollectionOfOne struct Dictionary struct EmptyCollection struct FilterCollectionView struct LazyBidirectionalCollection struct LazyForwardCollection struct LazyRandomAccessCollection struct MapCollectionView struct RandomAccessReverseView struct Range struct Repeat struct String struct UnsafeArray protocol MutableCollection
MutableCollection
Any algorithm or method that requires a type that adheres to the Collection or a Sequence protocols can not only accept any of the types listed above but also any types that adopt the MutableCollection protocol. These include the following:struct Array struct ContiguousArray struct Slice protocol MutableSliceable
MutableSliceable
And so it goes down the chain, so that the following also inherits the Sequence protocol through its inherited adoption of the Collection protocol (via MutableSliceable and MutableCollection):protocol ArrayType
ArrayType
Before the list of protocols adopting and inheriting comes to an end and we are left with the final types that inherit the Sequence protocol (through the Collection one, via ArrayType, MutableSliceable and MutableCollection) and exist at the end of the chain.struct ContiguousArray struct Slice struct Array<T>
Comments
Post a Comment