如果我有两个协议,其关联类型恰好相同,例如
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)")
}
}
但编译器抱怨并建议更改String
为Int
. 理想情况下,应该推断类型,或者如果我明确声明至少可以编译
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中是可能的,它也支持关联类型(尽管这不是惯用的用法)。