Arrays 当数组';s是另一个数组的元素

Arrays 当数组';s是另一个数组的元素,arrays,swift,where,where-clause,associated-types,Arrays,Swift,Where,Where Clause,Associated Types,我有一个数组,在数组中,我想约束内部数组的元素类型 // this doesn't work but should illustrate what I want. Is there // any way to constrain the Element type for an Array when it's // already declared as the type for an Array extension Array where Element == Array<Element

我有一个数组,在数组中,我想约束内部数组的元素类型

// this doesn't work but should illustrate what I want. Is there
// any way to constrain the Element type for an Array when it's
// already declared as the type for an Array 
extension Array where Element == Array<Element2: SomeProtocol> { 
}
//这不起作用,但应该说明我想要什么。有
//当数组处于活动状态时,是否可以约束数组的元素类型
//已声明为数组的类型
扩展数组,其中元素==数组{
}
最终,我认为解决这个问题将修复以下编译错误

protocol Thingy {
    associatedtype MeasurementType: Measurement

    var topMeasurements: [[MeasurementType]] { get }
}

extension Thingy {
    func doStuff() {

        // COMPILE ERROR'[[Self.MeasurementType]]' is not convertible to 'Array<Array<Measurement>>'
        let compileError = self.topMeasurements.measurements(forUUIDs: ["A"])

        // Hack workaround
        // The fact that this works leads me to believe that the constraint in the
        // extension is inadequate. The extension wants [[Measurement]] and 
        // event though MeasurementType is a kind of Measurement the compiler won't 
        // allow it. The hope is that if we could write something like 
        // [[Element]] where Element: Measurement
        let measurements: [[Measurement]] = self.topMeasurements
        let thisWorks = measurements.doSomething(ids: ["A"])
        print(thisWorks)
    }
}

// I'm hoping that if I constrain the following that it'll fix my issue
// constrain this type ----------------------
//                                          |
//                                          \/
extension Array where Element == Array<Measurement> {
    func doSomething(ids: [String]) -> [Measurement] {
        // do something return some elements, 
        // I've removed any code that could cause confusion
        return []
    }
}
protocolthingy{
关联类型度量值类型:度量值
var topMeasurements:[[MeasurementType]{get}
}
延伸物{
func doStuff(){
//编译错误“[[Self.MeasurementType]]”不能转换为“Array”
让compileError=self.topMeasurements.measurements(forUUIDs:[“A”])
//黑客解决方法
//这一事实让我相信
//扩展不充分。扩展需要[[Measurement]]和
//事件虽然MeasurementType是编译器不会使用的一种度量
//允许吧,希望我们能写下这样的东西
//[[Element]]其中Element:Measurement
让测量:[[Measurement]]=self.topMeasures
让thisWorks=measures.doSomething(id:[“A”])
印刷品(此作品)
}
}
//我希望,如果我限制以下内容,它将解决我的问题
//约束此类型----------------------
//                                          |
//                                          \/
扩展数组,其中元素==数组{
func doSomething(id:[字符串])->[测量]{
//做点什么,返回一些元素,
//我已经删除了任何可能引起混乱的代码
返回[]
}
}

作为所有集合上的扩展,而不仅仅是阵列上的扩展,这要容易得多。我相信您想要的扩展是:

extension Collection where Element: Collection, Element.Element: Measurement {
    func measurements(forUUIDs: [String]) -> [Measurement] {
        return []
    }
}

基本问题是Swift缺少更高级的类型,因此无法扩展
数组
(因为这不是一个合适的类型)。但是您可以扩展
集合(因为这是一个PAT)。

度量(forUUIDs:)
确实不清楚。我看到了代码是什么,但我仍然不知道它在做什么。我已将测量值更改为doSomething(ID:),我希望这能消除任何混淆。混淆不会影响问题,它会影响下一个阅读代码的人,很可能是你自己。帕特?我以前从未听过这个词。我试着找它,但你可能已经猜到了,我得到了一堆垃圾。PAT代表什么?@Biclops:一个有关联类型的协议这是另一个由这个问题引发的问题。也许你可以看看这个