Ich habe ein Problem mit der Kompilierung Tests Fälle in Swift. Es sieht so aus, als ob der Compiler Informationen über den Schablonentyp verliert, aber andere generische Methoden funktionieren gut. Was vermisse ich?Swift allgemeine Funktion kompiliert nicht im Test
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)'
}
}
aktualisieren
MatchNormElement ist ein Protokoll, also habe ich es konkreter. Jetzt funktioniert es.
func testMatchNorm1() {
var list = [Measurment]()
// works fine
let ll = MatchNorm.resolve1(list, lti: LinearTransformation(1), accuracy: 0.001)
}
Ich ersetzte 'MatchNormElement' und' LinearTransformation' mit 'NSString' (und entfernte' resolve2', diese Methode wird nicht benötigt, um das Problem zu finden), und es gibt keine Kompilierungsfehler. Vielleicht wird diese Information Ihnen helfen. –
Danke ShadowOf. MatchNormElement ist ein Protokoll. Und es sieht so aus, als müsste die Liste aus konkreten Typen bestehen. –