通过将实例属性类型与通用参数进行比较来约束通用函数

时间:2019-03-05 16:06:22

标签: swift generics generic-programming

我想将ViewControllers子类化以创建一个通用的Coordinator类。该协调器类应该能够安全地键入对子协调器类的依赖关系。子协调员只能访问显式定义的依赖项。我使用抽象类制作了以下工作环境,以布局问题。我对其他如何解决所描述问题的想法持开放态度。

先决条件

import Foundation

protocol HasFirstDependency {
    var first: Any? { get }
}

protocol HasSecondDependency {
    var second: Any? { get }
}

typealias AllDependencies = HasFirstDependency & HasSecondDependency

struct AppDependencies: AllDependencies {
    var first: Any?
    var second: Any?
}



class Coordinator<D> {
    var dependencies: D?
}

extension Coordinator {

    static func initialize() -> Coordinator<D> {
        return Coordinator<D>()
    }

}

class ParentCoordinator: Coordinator<AllDependencies> {
    var children: [Any] = []
}

class FirstChildCoordinator: Coordinator<HasFirstDependency> {}

class SecondChildCoordinator: Coordinator<HasSecondDependency> {}

问题

以下代码概述了问题(请参阅注释1和2)。无论如何,都可以避免在描述时强制转换并以描述的方式限制childType吗?

extension ParentCoordinator {

    func add<C: Coordinator<D>, D>(childType: C.Type) { // 2. ... by setting a constraint like this: "where self.dependecies is D?"
        let child = C.initialize()
        children.append(child)
        if let dependencies: D? = self.dependencies as? D? { // 1. is there any way to avoid this casting ...
            child.dependencies = dependencies
        } else {
            assertionFailure("parentCoordinator does not hold child dependencies")
        }
    }

}

let parent = ParentCoordinator()
parent.dependencies = AppDependencies(first: "bla", second: "blup")
parent.add(childType: FirstChildCoordinator.self)
let child = parent.children.first as? Coordinator<HasFirstDependency>
print(type(of: parent.dependencies)) // Optional<HasFirstDependency & HasSecondDependency>
print(parent.dependencies?.first) // Optional("bla")
print(parent.dependencies?.second) // Optional("blup")
print(type(of: child?.dependencies)) // Optional<HasFirstDependency>
print(child?.dependencies?.first) // Optional("bla")
//print(child?.dependencies?.second) // does not compile, what I wanted to achieve

我想要的

更具体地说:以下致命错误是我想在编译时捕获的错误。

protocol ForgottenDependency {
    var forgotten: Any? { get }
}

class ThirdChildCoordinator: Coordinator<ForgottenDependency> {}

parent.add(childType: ThirdChildCoordinator.self) // Fatal error: parentCoordinator does not hold child dependencies: file MyPlayground.playground, line 49

1 个答案:

答案 0 :(得分:1)

您要请求的是关于类型约束的某种逻辑OR

... where D == HasFirstDependency || HasSecondDependency

不能快速分离类型约束。引用commonly rejected changes

  

类型约束中的析取(逻辑或):这些类型包括匿名的类联合类型(例如,可以由整数或字符串占用的类型的(整数|字符串)。 “ [这种约束是]类型系统不能也不应该支持的东西。”

一种可能的解决方案是使依赖项符合通用协议。老实说,这并不能完全解决检测“忘记的”依赖项的问题,因为所有依赖项都必须实现此通用协议。 here中讨论了这种方法。

相关问题