【问题标题】:How to solve this Coroutine circularly reference error?如何解决这个 Coroutine 循环引用错误?
【发布时间】:2019-09-12 11:33:58
【问题描述】:

我明天有一个关于协程的考试,但问题是那一章的讲座被取消了考试并且没有替代品。所以我决定自己研究协程,这很难。

我有老师的 Typescript 代码示例,但问题是示例包含错误,我不知道如何解决。

type Coroutine<s, e, a> = Fun<s, Either<NoRes<s, e, a>, Pair<a, s>>>
type NoRes<s, e, a> = Either<e, Continuation<s, e, a>>
type Continuation<s, e, a> = Pair<s, Coroutine<s, e, a>>

我得到的错误是:

类型别名 'Coroutine' 循环引用自身

类型别名“NoRes”循环引用自身

类型别名'Continuation'循环引用自身

我明白为什么会发生这种情况,因为CoroutineNoRes 类型,NoResContinuation 类型,现在我们回到开始完成循环:ContinuationCoroutine 类型.

我不明白如何解决这个问题,替代方案是如何实现协程。那么有没有人有比上面那个更好、更有效的例子呢?

依赖关系:

乐趣:

type Fun<a, b> = {
    f: (_: a) => b
    then: <c>(this: Fun<a, b>, g: Fun<b, c>) => Fun<a, c>
}

要么:

type Either<a, b> = {
    kind: "left"
    value: a
} | {
    kind: "right"
    value: b
}

对:

type Pair<a, b> = { First: a, Second: b }

【问题讨论】:

    标签: typescript types coroutine


    【解决方案1】:

    我明天有同样的考试,也遇到了错误。

    我设法通过创建一个专门为 Continued Coroutine 制作的接口来编译它,如下所示:

    type Coroutine<s,e,a> = Fun<s, Either<NoRes<s,e,a>,Pair<a,s>>>
    type NoRes<s,e,a> = Either<e,Continuation<s,e,a>>
    type Continuation<s,e,a> = Pair<s,ContinuedCoroutine<s,e,a>>
    
    interface ContinuedCoroutine<s,e,a> extends Coroutine<s,e,a> { }
    

    感觉像是一种解决方法,但它也会使单元编译。我仍在尝试找出连接、地图等。但是...

    This 可能会进一步帮助您。如果您找到更好的方法,请更新我;)

    【讨论】:

    • 祝你考试顺利
    猜你喜欢
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 2021-02-13
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多