编译器告诉我,
Expression type '([String]) -> Bool' is ambiguous without more context
我不知道如何将更多的上下文到语句。 我想扩展声明会告诉它需要对集合中的元素的功能应有尽有。 我离开的代码片段更多的心思。 寻找标记的区域“!编译错误!!!”
你应该能够将代码复制并粘贴到一个空的项目,以查看编译错误。
protocol Measurement {
var id: String { get }
var displayPriority: Int { get }
}
extension Collection where Element: Collection, Element.Element: Measurement {
func contains(ids: [String]) -> Bool {
return self.filter{ measurementCollection -> Bool in
// !!! COMPILE ERROR !!!
// Error: Expression type '([String]) -> Bool' is ambiguous without more context
//
// The compiler doesn't under stand that this is a collection of Measurement,
// Do I somehow need to tell the compiler that the elements to the collection
// or collections with elements with a type that is a kind of Measurement again?
// I thought the extension declaration already specified everything it needed.
return measurementCollection.isEquals(ids: ids)
}.count > 0
}
}
extension Collection where Element == Measurement {
func isEquals(ids: [String]) -> Bool {
// get all the IDs
let allIDs = self.map{ $0.id }
// convert it to a set to make comparisons easier
return Set(allIDs) == Set(ids)
}
}
func whatIwantToDo() {
let measurements = [[ConcreteMeasurement("A")],
[ConcreteMeasurement("B")],
[ConcreteMeasurement("C"), ConcreteMeasurement("D")]
]
let ids = ["C", "D"]
// Ultimately, I want to fix my compile error to run the following line of code, this should output to True
print("Does measurements contain: \(ids) -> \(measurements.contains(ids: ids))" )
}