如何创建引用自身的类型别名?

How to make a type alias that references itself?

我使用 RX Swift 创建了一些网关/提供商以与 API 中的 API 集成,我正在尝试处理我认为的分页喜欢干净简单的方式。

基本上,函数签名如下所示:

func getPlaces(with location: CLLocationCoordinate2D) -> Observable<(value: [Place], next: Observable<(value: [Places], Observable<(value: [Place], next: ... ... >>

这很快就显得不切实际,所以我尝试为此创建一个类型别名:

typealias Result = Observable<(value: [Place], next: Result?)>

所以我的函数签名看起来像这样:

func getPlaces(with location: CLLocationCoordinate2D) -> Result

但是 Xcode 不会那么容易被愚弄,并且因为在其内部引用我的类型别名而叫我出来

所以...它甚至可行吗?怎么样?

我认为使用 typealias 是不可能的,因为您正在创建无限类型。我能想到的唯一方法是使 Observable 成为递归枚举:

enum Observable {
   case end([Place])
   indirect case node([Place], Observable)
}

因此,我将我的方法与 Nibr 的方法混合使用一个案例。这允许从 ViewModel 端更简单地处理分页(在我看来)

enum Result<T> {
    indirect case node([T], Observable<Result>?)

    var value: [T] {
        if case let GatewayResult.node(value, _) = self {
            return value
        }
        return []
    }

    var next: Observable<Result>? {
        if case let GatewayResult.node(_, next) = self {
            return next
        }
        return nil
    }
}