我的代码遵循以下总体设计:
protocol DispatchType {}
class DispatchType1: DispatchType {}
class DispatchType2: DispatchType {}
func doBar<D:DispatchType>(value:D) {
print("general function called")
}
func doBar(value:DispatchType1) {
print("DispatchType1 called")
}
func doBar(value:DispatchType2) {
print("DispatchType2 called")
}
实际上 DispatchType
实际上是后端存储。 doBar
函数是依赖于正确存储类型的优化方法。如果我这样做,一切都会正常:
let d1 = DispatchType1()
let d2 = DispatchType2()
doBar(value: d1) // "DispatchType1 called"
doBar(value: d2) // "DispatchType2 called"
但是,如果我创建一个调用 doBar
的函数:
func test<D:DispatchType>(value:D) {
doBar(value: value)
}
我尝试了类似的调用模式,我得到:
test(value: d1) // "general function called"
test(value: d2) // "general function called"
这似乎是 Swift 应该能够处理的事情,因为它应该能够在编译时确定类型约束。作为一个快速测试,我还尝试将 doBar
编写为:
func doBar<D:DispatchType>(value:D) where D:DispatchType1 {
print("DispatchType1 called")
}
func doBar<D:DispatchType>(value:D) where D:DispatchType2 {
print("DispatchType2 called")
}
但得到相同的结果。
如果这是正确的 Swift 行为,有什么想法吗?如果是的话,有什么办法可以避免这种行为吗?
编辑 1:我试图避免使用协议(protocol)的示例。假设我有代码(从我的实际代码大大简化):
protocol Storage {
// ...
}
class Tensor<S:Storage> {
// ...
}
对于 Tensor
类,我有一组可以在 Tensor
上执行的基本操作。但是,操作本身会根据存储改变其行为。目前我通过以下方式完成此任务:
func dot<S:Storage>(_ lhs:Tensor<S>, _ rhs:Tensor<S>) -> Tensor<S> { ... }
虽然我可以将它们放入 Tensor
类中并使用扩展:
extension Tensor where S:CBlasStorage {
func dot(_ tensor:Tensor<S>) -> Tensor<S> {
// ...
}
}
这有一些我不喜欢的副作用:
我认为
dot(lhs, rhs)
优于lhs.dot(rhs)
。可以编写方便的函数来解决这个问题,但这会造成代码的巨大爆炸。这将导致
Tensor
类变得单一。我真的更喜欢让它包含最少量的必要代码并通过辅助功能扩展其功能。与 (2) 相关,这意味着任何想要添加新功能的人都必须接触基类,我认为这是糟糕的设计。
编辑 2:另一种选择是,如果您对所有内容都使用约束,事情就会按预期进行:
func test<D:DispatchType>(value:D) where D:DispatchType1 {
doBar(value: value)
}
func test<D:DispatchType>(value:D) where D:DispatchType2 {
doBar(value: value)
}
将导致调用正确的doBar
。这也并不理想,因为它会导致编写大量额外的代码,但至少让我保留当前的设计。
编辑 3:我发现文档显示了 static
关键字与泛型的使用。这至少对第 (1) 点有帮助:
class Tensor<S:Storage> {
// ...
static func cos(_ tensor:Tensor<S>) -> Tensor<S> {
// ...
}
}
允许你写:
let result = Tensor.cos(value)
并且它支持运算符重载:
let result = value1 + value2
它确实增加了所需的张量的详细程度。这可以做得更好一点:
typealias T<S:Storage> = Tensor<S>
最佳答案
这确实是正确的行为,因为重载决策是在编译时发生的(在运行时发生的操作将是一个相当昂贵的操作)。因此从内部test(value:)
,编译器唯一知道的事情 value
是它的某种类型符合 DispatchType
– 因此它可以分派(dispatch)的唯一重载是 func doBar<D : DispatchType>(value: D)
.
如果泛型函数始终由编译器专门化,情况就会有所不同,因为 test(value:)
的专门实现就会知道 value
的具体类型从而能够选择合适的过载。然而,泛型函数的专门化目前只是一种优化(因为如果没有内联,它可能会给您的代码带来显着的膨胀),因此这不会改变观察到的行为。
为了实现多态性,一种解决方案是通过添加 doBar()
来利用协议(protocol)见证表(请参阅 this great WWDC talk )。作为协议(protocol)要求,并在符合协议(protocol)的各个类中实现它的专门实现,而通用实现是协议(protocol)扩展的一部分。
这将允许动态调度 doBar()
,从而允许从 test(value:)
调用它并调用正确的实现。
protocol DispatchType {
func doBar()
}
extension DispatchType {
func doBar() {
print("general function called")
}
}
class DispatchType1: DispatchType {
func doBar() {
print("DispatchType1 called")
}
}
class DispatchType2: DispatchType {
func doBar() {
print("DispatchType2 called")
}
}
func test<D : DispatchType>(value: D) {
value.doBar()
}
let d1 = DispatchType1()
let d2 = DispatchType2()
test(value: d1) // "DispatchType1 called"
test(value: d2) // "DispatchType2 called"
关于swift - Swift 3 中通过间接调用调用了错误的专用泛型函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43214380/