I expected the following code to compile:

struct Wrapper<Element>: BidirectionalCollection {
    
    var elements: [Element]
    
    var startIndex: Int { return 0 }
    var endIndex: Int { return elements.count }
    
    func index(after index: Int) -> Int { return index + 1 }
    func index(before index: Int) -> Int { return index - 1 }
    
    subscript(position: Int) -> Element {
        return elements[position]
    }
    
}

However, I got a long list of errors. I tried adding this:

extension Wrapper {
    
    subscript(bounds: Range<Int>) -> BidirectionalSlice<Wrapper> {
        return BidirectionalSlice(base: self, bounds: bounds)
    }
    
}

But not Xcode shows an interesting error message: “Type 
`Wrapper<Element>.Index` does not conform to protocol `Comparable`”. Surely, 
Wrapper<Element>.Index is just Int which can be inferred from the startIndex 
property, for example. I tried setting the Index type explicitly:

struct Wrapper<Element>: BidirectionalCollection {
    typealias Index = Int
    ...
}

Now the code finally compiles. Bug?
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to