为什么下面的alternative1可以完美地工作?
宏当然是伪造的,仅用于说明目的:
func commonPrefixLength<T: Swift.Collection, U: Swift.Collection where
T: Sequence, U: Sequence,
T.GeneratorType.Element: Equatable,
T.GeneratorType.Element == U.GeneratorType.Element>
(collection1: T, collection2: U) -> T.IndexType.DistanceType {
var collection2generator = collection2.generate()
var i: T.IndexType.DistanceType = 0
for element1 in collection1 {
#if alternative1
let element2 = collection2generator.next()
if (element1 != element2) {
return i
}
#elseif alternative2
let optionalElement2 = collection2generator.next()
if let element2 = optionalElement2 {
if (element1 != element2) {
return i
}
}
else {
break
}
#endif
i++
}
return i
}
commonPrefixLength("abX", "abc")