2011-03-18 59 views
3

该代码给出编译错误:斯卡拉:申报时编译错误类型的延续任何=>没有

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?

+0

我不知道'loop'是否在做你认为正在做的事。试着写'()=> loop'或'loop _'来代替。 – 2011-03-18 23:54:37

+0

'loop'的唯一目的是无限地递归(并调用另一个方法,但为了简化示例,省略了这段代码)。 – 2011-03-19 00:21:27

回答

3

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

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

它看起来对我来说,编译器应该推断BNothing,但事实并非如此。

+1

'shift [Unit,Nothing,Nothing] {c => c()}'也可以。它绝对是continuations插件中的一个bug。 – 2011-03-19 08:53:08

1

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

Java调用void返回的是斯卡拉的Unit

欲了解更多信息,为什么你看不到詹姆斯·伊里关于Getting to the Bottom of Nothing at All要说什么。

+0

是的,但我不想**返回**没有。看看循环方法。它永远不会返回。我的问题是为什么我不能宣布这样的延续。 – 2011-03-18 21:33:33