Swift:扩展序列,其中Iterator.Element是泛型类型

时间:2018-08-23 07:28:57

标签: swift generics swift3 sequence where

我正在使用Xcode 8和Swift 3。

我正在尝试为QVector3D oldVec = QVector3D(x,y,z); //this is your 3x1 vertex position QVector4D newVec = QVector4D(oldVec.x(), oldVec.y(), oldVec.z(), 1); //added 1 as fourth component QVector4D transformedVec = matrix*newVec; //matrix is your 4x4 transformation matrix //transformedVec is now what you need but with the fourth component still being the 1, so we just leave it out: QVector3D transformedVec3D = QVector3D(transformedVec.x(), transformedVec.y(), transformedVec.z()); 编写扩展功能,但前提是元素类型为Sequence。下面是一个非扩展功能,显示了我想要的功能:

MyGenericClass<Value>

我尝试了以下操作:

public class MyGenericClass<Value> {
    public func generateValue() -> Value { ... }
}

public func myFunction<Value>(_ array: Array<MyGenericClass<Value>>) -> [Value] {
    return array.map { generic in generic.generateValue() }
}

但是它给了我错误:extension Sequence { public func myFunction<Value>() -> [Value] where Iterator.Element: MyGenericClass<Value> { return map { generic in generic.generateValue() } } } 的地图关闭。我猜是因为编译器实际上不是从函数主体中的where子句约束中推断元素类型?

0 个答案:

没有答案