該代碼給出編譯錯誤:斯卡拉:申報時編譯錯誤類型的延續任何=>沒有
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 co Any類型的ntinuations => Nothing?
我不知道'loop'是否在做你認爲正在做的事。試着寫'()=> loop'或'loop _'來代替。 – 2011-03-18 23:54:37
'loop'的唯一目的是無限地遞歸(並調用另一個方法,但爲了簡化示例,省略了這段代碼)。 – 2011-03-19 00:21:27