0
我在Swift中編譯測試用例時遇到了問題。它看起來像編譯器丟失有關模板類型的信息,但其他通用方法工作正常。我錯過了什麼?Swift通用函數不能在測試中編譯
public class MatchNorm {
public static func resolve1<T:SequenceType where T.Generator.Element:MatchNormElement>(list: T, lti: LinearTransformation, accuracy: Double) -> LinearTransformation {
// no problem
return MatchNorm.resolve1(list, lti: lti, accuracy: accuracy)
}
public static func resolve2<T:SequenceType where T.Generator.Element:MatchNormElement>(list: T, lti: LinearTransformation, accuracy: Double) -> LinearTransformation {
for elem in list {
print(elem.x)
}
return lti
}
}
public class MatchNormTest: XCTestCase {
func testMatchNorm1() {
var list = [MatchNormElement]()
// compilation error here!
let ll = MatchNorm.resolve1(list, lti: LinearTransformation(1), accuracy: 0.001)
// MatchNormTest.swift:70:29: Cannot invoke 'resolve1' with an argument list of type '([MatchNormElement], lti: LinearTransformation, accuracy: Double)'
// MatchNormTest.swift:70:29: Expected an argument list of type '(T, lti: LinearTransformation, accuracy: Double)'
}
}
更新
MatchNormElement是一個協議,所以我改成了具體類型。現在它可以工作。
func testMatchNorm1() {
var list = [Measurment]()
// works fine
let ll = MatchNorm.resolve1(list, lti: LinearTransformation(1), accuracy: 0.001)
}
我用'NSString'替換了'MatchNormElement'和'LinearTransformation'(並且刪除了'resolve2',這個方法對於查找問題不需要),並且沒有編譯錯誤。也許這些信息會幫助你。 –
感謝ShadowOf。 MatchNormElement是一個協議。它看起來像列表必須具體的類型。 –