Need help with Continuations-Error “found cps expression in non-cps position”

六眼飞鱼酱① 提交于 2019-12-11 02:09:36

问题


I try building the following simple Generator using the Scala 2.8 Continuations-PlugIn. Where does the following error come from?

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)

Code:

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:


Those yieldValue calls are happening inside gen's constructor, which is not allowed (I assume). Ah, I just noticed you intended them to be the constructor parameter. Well, unfortunately, that syntax only works with methods. I'm not sure you don't get another error as well here.



来源:https://stackoverflow.com/questions/2646093/need-help-with-continuations-error-found-cps-expression-in-non-cps-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!