使自定义类型符合仅具有下标的 RandomAccessCollection

Conform a custom type to RandomAccessCollection with only a subscript

我通常实现行为类似于数组的类型,例如:

struct Dataset: RandomAccessCollection {
    let ids: [Int]
    // Other properties and methods...

    // Boilerplate
    var startIndex: Int { ids.startIndex }
    var endIndex: Int { ids.endIndex }
    func formIndex(after i: inout Int) { i += 1 }
    func formIndex(before i: inout Int) { i -= 1 }

    subscript(index: Int) -> Int {
        // Dummy example, could be more complex and return a different type
        return ids[index]
    }
}

问题是我每次都需要为 RandomAccessCollection 一致性编写大量样板代码。我想要一个协议或机制将样板减少到只有一个或两个要求:

此机制类似于当前在 Pytorch 中完成数据集继承的方式:仅具有 __len____getitem__ 要求。

我想出了一个这样的草稿:

protocol ArrayProtocol: RandomAccessCollection where Index == BaseCollection.Index {
    associatedtype BaseCollection: RandomAccessCollection
    
    var baseCollection: BaseCollection { get set }
    subscript(index: Index) -> Element { get set }
}

// Provide the default implementation of the RandomAccessCollection protocol
extension ArrayProtocol {
    var startIndex: Index { baseCollection.startIndex }
    var endIndex: Index { baseCollection.endIndex }
    func formIndex(after i: inout Index) { baseCollection.index(after: i) }
    func formIndex(before i: inout Index) { baseCollection.index(before: i) }
}

这个协议可以这样使用:

struct Dataset: ArrayProtocol {
    let ids: [Int]
    // Other properties and methods...

    // No more boilerplate
    var baseCollection: [Int] { ids }

    subscript(index: Int) -> Int {
        // Dummy example, could be more complex and return a different type
        return ids[index]
    }
}

但是我找不到让它工作的方法,我觉得关联类型不是一个很好的设计模式。

有什么解决办法吗?

编辑:where Index == BaseCollection.Index 子句不是必需的,下标可以具有与基础集合不同的 Index 类型。

关联类型一般用于泛型类型(通常是集合的元素)。为其添加关联类型typealias BaseCollection = [Int]并删除set的下标要求。

protocol ArrayProtocol: RandomAccessCollection where Index == BaseCollection.Index {
    associatedtype BaseCollection: RandomAccessCollection
    var baseCollection: BaseCollection { get set }
    subscript(index: Index) -> Element { get }
}

extension ArrayProtocol {
    var startIndex: Index { baseCollection.startIndex }
    var endIndex: Index { baseCollection.endIndex }
    func formIndex(after i: inout Index) { baseCollection.index(after: i) }
    func formIndex(before i: inout Index) { baseCollection.index(before: i) }
}

struct Dataset: ArrayProtocol {
    typealias BaseCollection = [Int]
    var baseCollection: BaseCollection = [ ]
    subscript(index: Int) -> Int { baseCollection[index] }
}


请注意,如果您想保持设置要求 subscript(index: Index) -> Element { get set },您还需要确保 BaseCollection 也符合 MutableCollection

subscript(index: Int) -> BaseCollection.Element {
    get { baseCollection[index] }
    set { baseCollection[index] = newValue } 
}

以上回答有误

func formIndex(after i: inout Index) { baseCollection.index(after: i) }
func formIndex(before i: inout Index) { baseCollection.index(before: i) }

应该是

func formIndex(after i: inout Index) { baseCollection.formIndex(after: &i) }
func formIndex(before i: inout Index) { baseCollection.formIndex(before: &i) }