我正在尝试构建一个协程框架,以通过并行遍历每个数据相关函数来启用批量数据获取。这是我到目前为止所拥有的:http: //pastie.org/7147798
这不起作用
def get(id: Long) = reset { // Is it not already cached? if (!cached.isDefinedAt(id)) { // Store the ID we want to fetch. queued += id // Come back later... shift { fetch[Object]() } : Seq[Any] @cps[ExecState[Object]] } // We should have the ID fetched now. Result(cached(id)) }
我收到以下错误
ashoat@ashoatmbp [~/project]# scala -P:continuations:enable Loader.scala /Users/ashoat/project/Loader.scala:134: error: type mismatch; found : Unit required: Any @util.continuations.package.cps[Main.$anon.Loader.ExecState[Main.$anon.Loader.Object]] if (!cached.isDefinedAt(id)) { ^ one error found
这有效
def get(id: Long) = reset { // Is it not already cached? if (!cached.isDefinedAt(id)) { // Store the ID we want to fetch. queued += id // Come back later... shift { fetch[Object]() } : Seq[Any] @cps[ExecState[Object]] // We should have the ID fetched now. Result(cached(id)) } else { // We should have the ID fetched now. Result(cached(id)) } }
这不起作用
val getFive = reset { if (true) { Result(5) } else { val seq: Seq[Any] = shift { fetch[Int](Object.get(15181990251L)) } val Seq(obj: Object) = seq Result(obj.fields("test").toInt) } }
我收到以下错误
ashoat@ashoatmbp [~/project]# scala -P:continuations:enable Loader.scala /Users/ashoat/project/Loader.scala:170: error: cannot cps-transform expression new this.Loader.Result[Int](5): type arguments [this.Loader.Result[Int],this.Loader.Result[Int],Nothing] do not conform to method shiftUnit's type parameter bounds [A,B,C >: B] Result(5)// : Result[Int] @cps[Result[Int]] ^ one error found
这有效
val getFive = reset { if (true) { Result(5) : Result[Int] @cps[Result[Int]] } else { val seq: Seq[Any] = shift { fetch[Int](Object.get(15181990251L)) } val Seq(obj: Object) = seq Result(obj.fields("test").toInt) } }
但我收到以下警告
ashoat@ashoatmbp [~/project]# scala -P:continuations:enable Loader.scala /Users/ashoat/project/Loader.scala:170: warning: expression (new this.Loader.Result[Int](5): this.Loader.Result[Int]) is cps-transformed unexpectedly Result(5) : Result[Int] @cps[Result[Int]] ^ one warning found 8