为什么`Future#toString`返回`“" List()“`? [英] Why does `Future#toString` returns `"List()"`?
问题描述
将来调用.toString
而不等待完成将导致不确定的结果.我的问题是为什么在未完成的期货中调用.toString
在scala 2.10.x和2.11.x中返回"List()"
? 实现似乎并不明确关于那个.
Calling .toString
on a future without waiting for complession leads to nondeterministic results. My question is why calling .toString
on uncompleted futures returns "List()"
in scala 2.10.x and 2.11.x? The implementation does not seem to be explicit about that.
从REPL中可以观察到此行为:
This behavior can be observed from the 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>)"
((
Note that Scala 2.12.x will likely explicitly implement Future#toString
to return "Future(<not completed>)"
instead (source).
证据表明这不是来自REPL的伪像或某些隐藏的隐式"(-Yno-predef
删除了所有默认的隐式):
evidence that this is not an artefact comming from the REPL or "some hidden implicit" (-Yno-predef
removes all the default implicits):
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")
推荐答案
这是删除sun.misc.Unsafe
依赖项的不幸副作用.在Scala 2.12和更高版本的Scala 2.11 IIRC中已得到纠正.
It was an unfortunate side effect of removing the dependence of sun.misc.Unsafe
. It is corrected in Scala 2.12 and newer versions of Scala 2.11 IIRC.
这篇关于为什么`Future#toString`返回`“" List()“`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!