Scala 需要继续错误方面的帮助“;在非cps位置发现cps表达”;
我尝试使用Scala 2.8 Continuations插件构建以下简单生成器。 以下错误来自何处Scala 需要继续错误方面的帮助“;在非cps位置发现cps表达”;,scala,scala-2.8,continuations,Scala,Scala 2.8,Continuations,我尝试使用Scala 2.8 Continuations插件构建以下简单生成器。 以下错误来自何处 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
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)
代码:
导入scala.util.continuations_
最温柔的对象{
val gen=新发电机1[Int]{
产量价值(1)
产量价值(2)
产量价值(3)
}
def main(参数:数组[字符串]):单位={
对于(v单位@cps[单位]){
变量循环:(E=>Unit)=null
def foreach(f:=>(E=>单位)):单位={
循环=f
重置[单位,单位](发电机)
}
def yieldValue(值:E):单位@cps[单位]=
移位{genK:(单位=>Unit)=>
循环(值)
耿(())
()
}
}
那些yieldValue
调用发生在gen
的构造函数中,这是不允许的(我假设).Ah,我刚刚注意到您打算将它们作为构造函数参数。很遗憾,这种语法只适用于方法。我不确定您在这里不会遇到另一个错误。谢谢,我为构造函数创建了一个关于按名称参数的问题:编译器插件似乎真的不允许这样做,有机会查询tiark v吗你的行李溢了吗?
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( () )
()
}
}