2010-04-15 65 views
2

我尝试使用Scala 2.8 Continuations-PlugIn构建以下简单的生成器。 以下错误来自哪里?需要Continuations-Error的帮助“在非cps位置找到cps表达式”

None/None/Some((Unit,Unit)) 
GenTest.scala:8: error: found cps expression in non-cps position 
     yieldValue(1) 

None/None/Some((Unit,Unit)) 
GenTest.scala:9: error: found cps expression in non-cps position 
     yieldValue(2) 

None/None/Some((Unit,Unit)) 
GenTest.scala:10: error: found cps expression in non-cps position 
     yieldValue(3) 

代码:

import scala.util.continuations._ 

object GenTest { 

    val gen = new Generator1[Int] { 
     yieldValue(1) 
     yieldValue(2) 
     yieldValue(3) 
    } 

    def main(args: Array[String]): Unit = { 
     for (v <- gen) { 
      println(v) 
     } 
    } 
} 



class Generator1[E](gen: => Unit @cps[Unit]) { 

    var loop: (E => Unit) = null 

    def foreach(f: => (E => Unit)): Unit = { 
     loop = f 
     reset[Unit,Unit](gen) 
    } 

    def yieldValue(value: E): Unit @cps[Unit] = 
    shift { genK: (Unit => Unit) => 
     loop(value) 
     genK(()) 
    () 
    } 
} 

回答

1

那些yieldValue呼叫gen的构造,这是不允许的(我认为)内发生的事情。啊,我只注意到你打算将它们作为构造函数参数。那么,不幸的是,该语法只适用于方法。我不确定你在这里还没有得到另一个错误。

+0

谢谢,我创建了一个关于构造函数的名称参数的问题:http://stackoverflow.com/questions/264714​​1/by-name-parameters-for-constructors – hotzen 2010-04-15 16:31:59

+0

它真的好像不被编译器 - 插件,通过stackoverflow查询tiark的任何机会? – hotzen 2010-04-16 18:18:50