我基本上遵循Scala API 页面上给出的用于分隔延续的示例。下面的代码工作正常:
import scala.util.continuations._
import scala.collection.mutable.HashMap
val sessions = new HashMap[Int, Int=>Unit]
def ask(prompt: String): Int @cps[Unit] = shift {
ret: (Int => Unit) => {
val id = sessions.size
printf("%s\nrespond with: submit(0x%x, ...)\n", prompt, id)
sessions += id -> ret
}
}
def submit(id: Int, addend: Int): Unit = {
sessions.get(id) match {
case Some(continueWith) => continueWith(addend)
}
}
def go = reset {
println("Welcome!")
val first = ask("Please give me a number")
val second = ask("Please enter another number")
printf("The sum of your numbers is: %d\n", first + second)
}
但是,当我修改go
为以下内容时:
def go = reset {
println("Welcome!")
List("First?","Second?").map[Int @cps[Unit]](ask)
}
我收到此错误:
error: wrong number of type parameters for method map: [B, That](f: String => B)
(implicit bf: scala.collection.generic.CanBuildFrom[List[String],B,That])That
List("First?","Second?").map[Int @cps[Unit]](ask)
^
添加Any
为第二种类型参数没有帮助。知道我应该提供什么类型吗?