【发布时间】:2019-10-07 12:14:14
【问题描述】:
我想在一个协议中定义函数,它有一个关联类型,只有在协议的关联类型遵循特定协议时才需要实现。
protocol Graph {
associatedtype N:GraphNode
// ... other functions
// This method shall only need implementation if N:Equatable
mutating func removeNode(_ node: N)
}
protocol GraphNode {
var id: Int { get }
}
扩展允许为特定情况添加功能,但也需要实现这些功能,我不想这样做。 所以这对我没有帮助:
extension Graph where N:Equatable {
mutating func removeNode(_ node: N) {
// Now I need to provide a default implementation
}
}
我需要更多这样的东西:
protocol Graph {
associatedtype N:GraphNode
// ...
mutating func removeNode(_ node: N) where N:Equatable
}
有什么办法吗?
提前致谢! :)
【问题讨论】: