Scala:声明类型Any=>;的延续时发生编译错误;没有什么

Scala:声明类型Any=>;的延续时发生编译错误;没有什么,scala,continuations,Scala,Continuations,此代码给出编译错误: 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 : (

此代码给出编译错误:

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编译器不喜欢Any=>Nothing类型的延续?

您不能返回类型
Nothing
,因为它没有实例。任何预期将不返回任何内容的代码决不能返回。例如,总是抛出异常的方法可以声明为不返回任何内容

Java调用的
void
的返回值在Scala中是
Unit


有关更多信息,请参见James Iry所说的内容。

如果我指定类型参数,它将编译:

shift[Unit, Nothing, Nothing] {c: (Unit => Nothing) => c()}

在我看来,编译器应该推断
B
Nothing
,但它不是。

是的,但我不想什么都不返回。看看循环方法。它永远不会回来。我的问题是为什么我不能声明这样一个延续。我想知道
loop
是否在做你认为它在做的事情。尝试编写
()=>循环
循环
循环
的唯一目的是无休止地递归(并调用另一个方法,但此代码被省略以简化示例)。
shift[Unit,Nothing,Nothing]{c=>c()}
也有效。这肯定是continuations插件中的一个bug。