بحاجة إلى مساعدة في الاستمرارية-الأخطاء "تم العثور على تعبير CPS في وضع غير CPS"
-
27-09-2019 - |
سؤال
أحاول بناء المولد البسيط التالي باستخدام Scala 2.8 Continuations-Plugin. من أين يأتي الخطأ التالي؟
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( () )
()
}
}
المحلول
هؤلاء yieldValue
تحدث المكالمات في الداخل gen
مُنشئ ، وهو غير مسموح به (أفترض). آه ، لقد لاحظت فقط أنك تعتزم أن تكون معلمة المنشئ. حسنًا ، لسوء الحظ ، هذا الجملة لا يعمل إلا مع الطرق. لست متأكدًا من أنك لا تحصل على خطأ آخر هنا.
لا تنتمي إلى StackOverflow