对 Collection 扩展函数的调用不明确
Ambiguous call to Collection extension function
编译器告诉我
Expression type '([String]) -> Bool' is ambiguous without more context
我不确定如何为声明添加更多上下文。我认为扩展声明会告诉函数它需要的关于集合中元素的一切。我在代码片段中留下了更多想法。查找标记为“!!! COMPILE ERROR !!!”的区域
您应该能够将代码复制并粘贴到空项目中以查看编译错误。
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{ [=11=].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))" )
}
第一个扩展具有约束 Element.Element: Measurement
,
这意味着在闭包中,measurementCollection
是一个集合,其
元素类型 采用 Measurement
协议。
因此,为了从该集合的第二个扩展中调用 isEquals()
方法,必须将其定义为
extension Collection where Element: Measurement { // Not: Element == Measurement
func isEquals(ids: [String]) -> Bool { ... }
}
另请注意,通常,测试
filter { ... }.count > 0
比
更有效率
contains(where: { ... })
编译器告诉我
Expression type '([String]) -> Bool' is ambiguous without more context
我不确定如何为声明添加更多上下文。我认为扩展声明会告诉函数它需要的关于集合中元素的一切。我在代码片段中留下了更多想法。查找标记为“!!! COMPILE ERROR !!!”的区域
您应该能够将代码复制并粘贴到空项目中以查看编译错误。
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{ [=11=].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))" )
}
第一个扩展具有约束 Element.Element: Measurement
,
这意味着在闭包中,measurementCollection
是一个集合,其
元素类型 采用 Measurement
协议。
因此,为了从该集合的第二个扩展中调用 isEquals()
方法,必须将其定义为
extension Collection where Element: Measurement { // Not: Element == Measurement
func isEquals(ids: [String]) -> Bool { ... }
}
另请注意,通常,测试
filter { ... }.count > 0
比
更有效率contains(where: { ... })