【发布时间】: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'循环引用自身
我明白为什么会发生这种情况,因为Coroutine 是NoRes 类型,NoRes 是Continuation 类型,现在我们回到开始完成循环:Continuation 是Coroutine 类型.
我不明白如何解决这个问题,替代方案是如何实现协程。那么有没有人有比上面那个更好、更有效的例子呢?
依赖关系:
乐趣:
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