Scala continuations未编译
我正在尝试Scala的延续库(使用Scala 2.12.10)。我正在测试的代码——检查我是否理解这个概念——如下所示:Scala continuations未编译,scala,continuations,Scala,Continuations,我正在尝试Scala的延续库(使用Scala 2.12.10)。我正在测试的代码——检查我是否理解这个概念——如下所示: object Test { import scala.util.continuations._ def run(): (Unit => Unit) = { var x = 0; reset { while (x < 10) { if (x == 5) shift { cont: (Uni
object Test {
import scala.util.continuations._
def run(): (Unit => Unit) = {
var x = 0;
reset {
while (x < 10) {
if (x == 5)
shift { cont: (Unit => Unit) =>
return cont
}
println(f"x = $x")
x += 1
}
}
}
def main(args: Array[String]): Unit = {
val cont = run()
cont()
}
}
通过将
else
分支添加到reset
内的条件中,并将run
的返回类型设置为Any
,我可以使代码正常工作。我认为这个问题与run
中不同代码分支返回的类型不一致有关,但我真的不知道是否有比Any
更具体的类型可以用来实现这个功能
object Test {
import scala.util.continuations._
def run():Any = {
var x = 0;
reset {
while (x < 10) {
if (x == 5)
shift { cont: (Unit => Unit) =>
return cont
} else shift { cont: (Unit => Unit) =>
cont()
}
println(f"x = $x")
x += 1
}
}
}
def main(args: Array[String]): Unit = {
val cont = run()
printf("Continuation called\n")
cont.asInstanceOf[Unit=>Unit]()
}
}
对象测试{
导入scala.util.continuations_
def run():任意={
var x=0;
重置{
而(x<10){
如果(x==5)
移位{cont:(单位=>Unit)=>
返回控制
}else移位{cont:(单位=>Unit)=>
续(
}
println(f“x=$x”)
x+=1
}
}
}
def main(参数:数组[字符串]):单位={
val cont=run()
printf(“称为延续的\n”)
cont.asInstanceOf[Unit=>Unit]()
}
}
object Test {
import scala.util.continuations._
def run():Any = {
var x = 0;
reset {
while (x < 10) {
if (x == 5)
shift { cont: (Unit => Unit) =>
return cont
} else shift { cont: (Unit => Unit) =>
cont()
}
println(f"x = $x")
x += 1
}
}
}
def main(args: Array[String]): Unit = {
val cont = run()
printf("Continuation called\n")
cont.asInstanceOf[Unit=>Unit]()
}
}