5

如果我有两个协议,其关联类型恰好相同,例如

protocol Read {
    associatedtype Element
    func read() -> Element
}
protocol Write {
    associatedtype Element
    func write(a: Element)
}

然后我想要一个类来读取整数并将字符串写入:

class ReadWrite: Read, Write {
    func read() -> Int {
        return 5
    }
    func write(a: String) {
        print("writing \(a)")
    }
}

但编译器抱怨并建议更改StringInt. 理想情况下,应该推断类型,或者如果我明确声明至少可以编译

associatedtype Read.Element = Int
associatedtype Write.Element = String

ReadWrite。有什么解决办法吗?

更新

受此问题启发的解决方法是创建两个辅助协议

protocol ReadInt: Read {
    associatedtype Element = Int
}
protocol WriteString: Write {
    associatedtype Element = String
}

并让类从这两个继承:

class ReadWrite: ReadInt, WriteString {
    func read() -> Int {
        return 5
    }
    func write(a: String) {
        print("writing \(a)")
    }
}

这似乎可以编译,但我担心这种方式会出现任何问题。

再次更新

我在 Swift 的问题跟踪器中发现了这个问题。任何需要这个缺失功能​​的人(比如我)都应该投票给它。作为比较,这种模式在Rust中是可能的,它也支持关联类型(尽管这不是惯用的用法)。

4

1 回答 1

1

另一种解决方法是创建第三个组合协议:

protocol ReadWrite {
    associatedtype R
    associatedtype W
    func read() -> R
    func write(a: W)
}

它并不漂亮,因为它迫使你重新声明协议成员,但它确实保持它的通用性(你不限于 String 和 Int)。

于 2016-06-10T04:35:27.360 回答