我正在尝试使用 SwiftHamcrest
我有一个函数
func equalToArray<T, S>(_ vector:Array<S>) -> Matcher<T> {
let v: Matcher<T> = Hamcrest.hasCount(16)
return v
}
这会报错
Error:(16, 31) 'hasCount' produces 'Matcher<T>', not the expected contextual result type 'Matcher<T>'
SwiftHamcrest 有两个 hasCount 函数
public func hasCount<T: Collection>(_ matcher: Matcher<T.IndexDistance>) -> Matcher<T>
public func hasCount<T: Collection>(_ expectedCount: T.IndexDistance) -> Matcher<T>
为什么我的代码没有返回所需的相同类型。
作为一个注释,可能还有一个不同的问题,我不得不添加 Hamcrest。在 hasCount 方法调用之前,否则它会尝试匹配第一个函数
我缺少什么类型?
最佳答案
你的方法 equalToArray<T, S>
不知道T
是一个集合,所以通用 hasCount(...)
的结果上述方法不能分配给 v
在你的方法中(因为这些结果返回 Matcher<T>
实例限制为 T
:s 即 Collection
:s)。即 v
属于 Matcher<T>
类型对于不受约束的 T
,意思是,在编译器的眼中,有例如没有T.IndexDistance
对于 T
的 v
:s 类型。
如果你添加一个Collection
T
的类型约束你的方法,来自 hasCount(...)
的作业结果为 v
应该编译:
func equalToArray<T: Collection, S>(_ vector: Array<S>) -> Matcher<T> {
let v: Matcher<T> = Hamcrest.hasCount(16)
return v
}
在一个完美的世界里,编译器可以给我们一个更有说服力的错误信息,比如
Error:(16, 31) '
hasCount
' produces 'Matcher<T>
' where 'T: Collection
', not the expected contextual result type 'Matcher<T>
'
现在,我不知道你打算在这里测试什么,但正如@Hamish 指出的那样,你可能实际上想要返回一个 Matcher<[S]>
并删除 T
占位符。例如。使用 count
所提供的属性 vector
参数作为 hasCount(...)
的参数?
func equalToArray<S>(_ vector: Array<S>) -> Matcher<[S]> {
return hasCount(vector.count)
}
我自己没有使用过 Hamcrest,我可能会弄错,但基于对 SwiftHamcrest 文档的快速浏览,我相信 equalToArray(_:)
如上定义将仅基于两个向量的计数为“向量相等性”(w.r.t. 函数名称的语义)构造一个匹配器,在这种情况下,以下断言将成功
let arr1 = ["foo", "bar"]
let arr2 = ["bar", "baz"]
assertThat(arr1, equalToArray(arr2)) // success! ...
但这只是署名,因为您没有向我们展示您打算应用 equalToArray(_:)
的上下文方法/匹配器;也许您只是向我们展示了一个最小的示例,而您的自定义匹配器的实际主体更符合方法的名称。
关于Swift 找不到正确的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43456245/