在未等待复合的情况下调用.toString
未来会导致不确定的结果。我的问题是为什么在未完成的期货上调用.toString
在scala 2.10.x和2.11.x中返回"List()"
? The implementation似乎并不明确。
可以从REPL中看到此行为:
scala> import scala.concurrent.Future, scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global
scala> Future(1).toString
res0: scala.concurrent.Future[Int] = Success(1)
scala> Future(1).toString
res1: scala.concurrent.Future[Int] = List()
请注意,Scala 2.12.x可能会明确实现Future#toString
而不是返回"Future(<not completed>)"
(source)。
编辑:证明这不是来自REPL或“隐藏隐藏”的人工制品(-Yno-predef
删除所有默认隐含):
Future.scala:
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
object Main extends App {
System.out.println(Future(1).toString)
}
build.sbt:
scalaVersion := "2.11.8"
scalacOptions := Seq(
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-unchecked",
"-Yno-predef",
"-Xfatal-warnings",
"-Xlint",
"-Yinline-warnings",
"-Yno-adapted-args",
"-Ywarn-dead-code",
"-Ywarn-unused-import",
"-Ywarn-numeric-widen",
"-Ywarn-value-discard",
"-Xfuture")
答案 0 :(得分:8)
消除sun.misc.Unsafe
的依赖是一个不幸的副作用。它在Scala 2.12和更新版本的Scala 2.11 IIRC中得到了纠正。