我有一个带有PAT的协议,该协议在扩展中提供了默认实现。 然后,符合该协议的类提供"覆盖"。 不调用此重写,编译器"首选"默认值。
//---- Definition of the protocol with a default implentation, because I am forced to.
protocol Bag: AnyObject {
associatedtype BagObject
func add(_ e: BagObject)
}
extension Bag where BagObject: Equatable {
// I here give a default implementation, because I can't do differently, as this
// is an extension, and it is an extension because it only applies to assoicated
// types that are Equatables
func contains(_ e: BagObject) -> Bool {
print("Default implementation is called")
return false
}
}
///---- Definition of a class that gives a concrete implementation of the protocol
class BagConcreteImplementation<BagObject>: Bag {
var bag = Array<BagObject>()
func add(_ e: BagObject) { bag.append(e) }
}
extension BagConcreteImplementation where BagObject: Equatable {
// I here give a concrete implementation when the generic type is Equatable
func contains(_ e: BagObject) -> Bool {
print("Concrete implementation is called")
return bag.contains(e)
}
}
///---- This is a class that encapsulate a bag, in real life, this class is adding some filtering to objects that can be added to the bag
class AClassThatHaveABag<BagType: Bag> {
typealias BagObject = BagType.BagObject
let myBag: BagType
init(bag: BagType) { myBag = bag }
}
extension AClassThatHaveABag where BagType.BagObject: Equatable {
func contains(_ e: BagObject) {
// The problem here is that the compiler only knows that myBag is a Bag
// of Int and therefore calls the default implementation
// It does not call the concrete implementation of the concrete class that WILL be provided
myBag.contains(e)
}
}
let aBagOfInt = BagConcreteImplementation<Int>()
let objectThatContainsABagOfInt = AClassThatHaveABag(bag: aBagOfInt)
aBagOfInt.contains(0)
objectThatContainsABagOfInt.contains(0)
// Prints
// Concrete implementation is called
// Default implementation is called
我们可以清楚地看到直接调用是调用具体实现,而封装调用是调用默认实现。
如何确保始终调用具体实现,即使通过封装?
我尝试了一些方法,不知何故这有效:
在协议中声明contains
,而不是在扩展中声明,如下所示:
func contains<T>(_ e: T) -> Bool where T: Equatable, T == BagObject
如果你在类中执行类似T == BagObject
的操作,则会出现编译器错误,说这使T
多余,但显然这在协议中是可以的。
然后,像这样实现默认实现:
extension Bag {
func contains<T>(_ e: T) -> Bool where T: Equatable, T == BagObject {
print("Default implementation is called")
return false
}
}
然后,您可以直接在类中实现具体实现,作为非泛型方法。
class BagConcreteImplementation<BagObject> : Bag {
func contains(_ e: BagObject) -> Bool where BagObject : Equatable {
print("Concrete implementation is called")
return bag.contains(e)
}
var bag = Array<BagObject>()
func add(_ e: BagObject) { bag.append(e) }
}
在不更改调用站点的情况下,代码将打印:
Concrete implementation is called
Concrete implementation is called
为了完整起见,我在这里发布了另一个解决方案,它避免使用我发现(我的拙见)不那么干净的T == BagObject
。
这个想法是使用Bag
的专用版本,称为BagOfEquatables
,并扩展类
protocol Bag: AnyObject {
associatedtype BagObject
func add(_ e: BagObject)
}
// Specialized version of Bag for Equatables
protocol BagOfEquatables: Bag where BagObject: Equatable{
func contains(_ e: BagObject) -> Bool
}
extension BagOfEquatables {
func contains(_ e: BagObject) -> Bool {
print("Default implementation is called")
return false
}
}
///---- Definition of a class that gives a concrete implementation of the protocol
class BagConcreteImplementation<BagObject>: Bag {
var bag = Array<BagObject>()
func add(_ e: BagObject) { bag.append(e) }
}
extension BagConcreteImplementation: BagOfEquatables where BagObject: Equatable {
func contains(_ e: BagObject) -> Bool {
print("Concrete implementation is called")
return bag.contains(e)
}
}
///---- This is a class that encapsulate a bag
class AClassThatHaveABag<BagType: Bag> {
typealias BagObject = BagType.BagObject
let myBag: BagType
init(bag: BagType) { myBag = bag }
}
extension AClassThatHaveABag where BagType: BagOfEquatables {
func contains(_ e: BagObject) -> Bool {
myBag.contains(e)
}
}
let aBagOfInt = BagConcreteImplementation<Int>()
let objectThatContainsABagOfInt = AClassThatHaveABag(bag: aBagOfInt)
aBagOfInt.contains(0)
objectThatContainsABagOfInt.contains(0)
// Prints
// Concrete implementation is called
// Concrete implementation is called
你说:
不调用此重写,编译器"首选"默认值。
问题是您已在扩展中提供了该方法的默认实现,但从未将其声明为协议本身的一部分。因此,编译器将使用"静态调度"。
如果在协议中包含该方法,编译器将使用"动态调度"来解析要调用的方法。
有关静态与动态调度的讨论,请参阅 WWDC 2016 了解 Swift 性能。
例如:
protocol Foo {
// without this declaration, this will use static dispatch;
// uncomment the following line for dynamic dispatch
// func foo()
}
extension Foo {
func foo() {
print("default")
}
}
struct Bar: Foo {
func foo() {
print("bar implementation")
}
}
let bar: Foo = Bar()
bar.foo() // “default” with static dispatch; ”bar implementation“ with dynamic dispatch