Created
June 22, 2016 10:01
-
-
Save cezheng/59c90a48fa13782482fe95fe87bb6339 to your computer and use it in GitHub Desktop.
Swift 3 Collection protocol bug on Xcode 8 beta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copy the following code into a playground on Xcode 8 beta | |
import Foundation | |
class CustomCollection: Collection { | |
typealias Index = Int | |
typealias IndexDistance = Int | |
private(set) var count: Int | |
private var cursor = 0 | |
func next() -> Int? { | |
defer { | |
cursor += 1 | |
} | |
if cursor < self.count { | |
return cursor | |
} | |
return nil | |
} | |
subscript(_ idx: Index) -> Int { | |
if idx < count { | |
return idx | |
} | |
return 0 | |
} | |
var startIndex: Index { | |
return 0 | |
} | |
var endIndex: Index { | |
return count | |
} | |
func index(after i: Index) -> Index { | |
return i + 1 | |
} | |
init(count: Int) { | |
self.count = count | |
} | |
} | |
// This class cannot use for loop on its instances | |
class DerivedCustomCollection : CustomCollection { | |
} | |
// This class cannot use for loop on instance.enumerated() | |
class DerivedCustomCollectionWithCustomIterator : CustomCollection { | |
typealias Iterator = IndexingIterator<DerivedCustomCollectionWithCustomIterator> | |
} | |
let collection1 = DerivedCustomCollection(count: 5) | |
let collection2 = DerivedCustomCollectionWithCustomIterator(count: 5) | |
// Swift Compiler Error: | |
// 'IndexingIterator<DerivedCustomCollection>' is not convertible to 'Iterator' (aka 'IndexingIterator<CustomCollection>') | |
for i in collection1 { | |
print(i) | |
} | |
// OK | |
for (o, i) in collection1.enumerated() { | |
print("\(o) \(i)") | |
} | |
// OK | |
for i in collection2 { | |
print(i) | |
} | |
// Swift Compiler Error: | |
// Expression type 'EnumeratedSequence<DerivedCustomCollectionWithCustomIterator>' is ambiguous without more context | |
for (o, i) in collection2.enumerated() { | |
print("\(o) \(i)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment