[swift-users] SubSequence Index bug in generic functions?

Hooman Mehr hooman at mac.com
Wed May 11 15:48:50 CDT 2016


The reason this is appearing here before filing a radar is getting a quick feedback as I have been assuming this is the expected behavior but Nate Cook told me otherwise. See this thread <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160509/017048.html> for the initial discussion that lead to this.

Run this code to see the issue: (Works with the latest Swift 3.0 snapshot)

    func subSequenceIndexBug
        
            <C: Collection
                where
                    C.Iterator.Element: Equatable,
                    C.SubSequence: Collection,
                    C.SubSequence.Index == C.Index,
                    C.SubSequence.Iterator.Element == C.Iterator.Element>
        
        (_ c: C, _ e: C.Iterator.Element)
    {
        let junction = c.index(c.startIndex, offsetBy: numericCast(5)) // Arbitrary split point.
        
        if let i = c.suffix(from: junction).index(of: e) {
        
            print("Searched for \"\(e)\" and found \"\(c[i])\". Result of testing them for equality is: \(e == c[i])")
        }
    }
    
    subSequenceIndexBug("ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters, "Q")
    // Prints: "Searched for "Q" and found "L". Result of testing them for equality is: false"

Is this a generics design limitation, my bug, or a compiler bug?

Thanks for your,
Hooman

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160511/47ddce03/attachment.html>


More information about the swift-users mailing list