具有特定类型元素的 CollectionType 的扩展忽略子类型
Extension for CollectionType with element of certain type ignores subtypes
我正在尝试为包含特定类型 FooType 的元素的集合类型创建扩展,这工作得很好:
extension CollectionType where Generator.Element == FooClass
{
func doSomething()
}
let collectionType = [FooClass]()
collectionType.doSomething()
我的问题是我还希望此扩展适用于包含 Foo class 的子classes 的集合类型 class:
let collectionType = [FooSubclass]()
collectionType.doSomething() // Doesn't work
我可能会覆盖“==”运算符并循环遍历超级 classes 链,直到找到匹配项,但不确定这是否是正确的方法。
public func ==(t0: Any.Type?, t1: Any.Type?) -> Bool
我是不是在我的 where 子句中遗漏了一些可以解决这个问题的东西,还是我这样做完全错了?
使用 :
而不是 ==
。
尝试:
extension CollectionType where Generator.Element: FooClass {
我正在尝试为包含特定类型 FooType 的元素的集合类型创建扩展,这工作得很好:
extension CollectionType where Generator.Element == FooClass
{
func doSomething()
}
let collectionType = [FooClass]()
collectionType.doSomething()
我的问题是我还希望此扩展适用于包含 Foo class 的子classes 的集合类型 class:
let collectionType = [FooSubclass]()
collectionType.doSomething() // Doesn't work
我可能会覆盖“==”运算符并循环遍历超级 classes 链,直到找到匹配项,但不确定这是否是正确的方法。
public func ==(t0: Any.Type?, t1: Any.Type?) -> Bool
我是不是在我的 where 子句中遗漏了一些可以解决这个问题的东西,还是我这样做完全错了?
使用 :
而不是 ==
。
尝试:
extension CollectionType where Generator.Element: FooClass {