如何在 Scala 中启用延续?

问题描述 投票:0回答:2

问题说明了一切。

(然而,如何访问

shift
reset
操作的详细信息多年来已发生变化。旧的博客条目和 Stack Overflow 答案可能包含过时的信息。)

另请参阅 什么是 Scala 延续以及为什么使用它们? 其中讨论了一旦您拥有

shift
reset
,您可能想用它们做什么。

scala continuations
2个回答
30
投票

Scala 2.12 或 2.11

最简单的方法是使用 sbt:

scalaVersion := "2.12.2"    
autoCompilerPlugins := true    
addCompilerPlugin(
  "org.scala-lang.plugins" % "scala-continuations-plugin" % "1.0.3" cross CrossVersion.patch)
libraryDependencies +=
  "org.scala-lang.plugins" %% "scala-continuations-library" % "1.0.3"
scalacOptions += "-P:continuations:enable"

在您的代码(或 REPL)中,执行

import scala.util.continuations._

您现在可以随心所欲地使用

shift
reset

Scala 2.8、2.9、2.10 的历史信息

您必须使用

-P:continuations:enable
标志启动 scala(或 scalac)。

在您的代码中,执行

import scala.util.continuations._

您现在可以随心所欲地使用

shift
reset

如果您使用的是 sbt 0.7,请参阅 https://groups.google.com/forum/#!topic/simple-build-tool/Uj-7zl9n3f4

如果您使用 sbt 0.11+,请参阅 https://gist.github.com/1302944

如果您使用的是 Maven,请参阅 http://scala-programming-language.1934581.n4.nabble.com/scala-using-continuations-plugin-with-2-8-0-RC1-and-maven- td2065949.html#a2065949


7
投票

非SBT解决方案:

scala -Xpluginsdir /.../scala/lib/ -P:continuations:enable

适用于 scala 2.11.6,但插件/库它将不再包含在 Scala 2.12 中

© www.soinside.com 2019 - 2024. All rights reserved.