【发布时间】:2011-03-18 17:55:21
【问题描述】:
此代码给出编译错误:
import scala.util.continuations._
object CTest {
def loop: Nothing = reset {
shift {c: (Unit => Nothing) => c()}
loop
}
def main(argv: Array[String]) {loop}
}
错误信息:
error: type mismatch;
found : ((Unit) => Nothing) => (Unit) => Nothing
required: ((Unit) => B) => (Unit) => Nothing
但此代码按预期工作:
import scala.util.continuations._
object CTest {
def loop: Nothing = reset {
shift {c: (Unit => Any) => c.asInstanceOf[Unit => Nothing]()}
loop
}
def main(argv: Array[String]) {loop}
}
问题是:为什么 Scala 编译器讨厌 me Any => Nothing 类型的延续?
【问题讨论】:
-
我想知道
loop是否正在做你认为它正在做的事情。尝试改写() => loop或loop _。 -
loop的唯一目的是无限递归(并调用其他方法,但为了简化示例,省略了这段代码)。
标签: scala continuations