Given the general structbased on struct...
struct MyCollection<Element>: CollectionType, MyProtocol {
typealias Index = MyIndex<MyCollection>
subscript(i: Index) -> Element { … }
func generate() -> IndexingGenerator<MyCollection> {
return IndexingGenerator(self)
}
}
... how to define Indexfor him ...
struct MyIndex<Collection: MyProtocol>: BidirectionalIndexType {
func predecessor() -> MyIndex { … }
func successor() -> MyIndex { … }
}
... without introducing a death dependency cycle?
A general character is MyIndexnecessary because:
- It should work with any type
MyProtocol. MyProtocollinks Selfand therefore can only be used as a type restriction.
If forward declarations (à la Objective-C) appeared, I would just [sic!] Add one for MyIndex<MyCollection>mine MyCollection<…>. Alas, this does not happen.
A possible specific use case is binary trees, such as:
indirect enum BinaryTree<Element>: CollectionType, BinaryTreeType {
typealias Index = BinaryTreeIndex<BinaryTree>
case Nil
case Node(BinaryTree, Element, BinaryTree)
subscript(i: Index) -> Element { … }
}
This will require Indexa stack based:
struct BinaryTreeIndex<BinaryTree: BinaryTreeType>: BidirectionalIndexType {
let stack: [BinaryTree]
func predecessor() -> BinaryTreeIndex { … }
func successor() -> BinaryTreeIndex { … }
}
(?) struct struct Swift.
BinaryTreeIndex<…> BinaryTree<…>.
BinaryTreeIndex,
BinaryTreeType.