如何实例化两个相互依赖的泛型类?

时间:2019-07-22 18:42:56

标签: swift generics nested-generics protocol-oriented

我有两个需要互相实例化的泛型类,是否有一种优雅的方式做到这一点? 请查看以下示例:

protocol Presentable {
    /// Protocol for view
}
protocol Action {
    /// Protocol for Presenter
}

class Presenter<T: Presentable> : Action {
    /// Presenter looking for a class that implementing the Presentable protocol
}

class SomeViewController<T: Action> : Presentable {
    /// SomeViewController looking for a class that implementing the Action protocol
}

如您所见,SomeViewController期望Presenter,而Presenter期望SomeViewController。 因此,创建其中一个是不可能的,因为我们将有无限循环

let vc = SomeViewController<Presenter<SomeViewController<Presenter>>> ...

是否有一种优雅的解决方法?

0 个答案:

没有答案