我试图建立一个协同框架,通过并行地逐步通过每个数据相关功能来启用批量数据获取。以下是我迄今为止:http://pastie.org/7147798斯卡拉CPS框架中的行为不一致
这不起作用
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)) }
我收到以下错误
[email protected] [~/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) } }
我收到以下错误
[email protected] [~/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) } }
但我得到以下警告
[email protected] [~/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
你为什么期望“现在获取ID”?我读取你的代码的方式,我看到'Result(...)'在* shift *块之后,也是* reset *中的最后一个表达式。所以我期望'shift'采用Result [Int]并用它计算另一个表达式,这将是整个* reset *块返回的值。 – huynhjl 2013-03-28 07:25:16
@Ashoat请大家帮忙解释一下你想达到的目标。获取函数的功能是什么?看起来你使用shift来调用外部过程而不是上下文转换。不想听起来粗鲁,但我必须问你是否对使用shift/reset有信心。 – 2013-03-28 08:17:04
@ pagoda_5b:我编辑了这个问题来添加一些更多的细节,包括到目前为止我已经完整源代码的链接。我对shift的使用只是将定界复位块的当前状态与一些指定的依赖关系一起传递给一个将它们转换为单个continuation的函数,然后将其返回给reset块的调用者。 – Ashoat 2013-03-28 09:17:25