运维开发网

scala – 为什么`Future#toString`返回`“List()”`?

运维开发网 https://www.qedev.com 2020-05-31 08:07 出处:网络 作者:运维开发网整理
在未等待compless的情况下调用.toString会导致不确定的结果.我的问题是为什么在未完成的期货上调用.toString会在 scala 2.10.x和2.11.x中返回“List()”? The implementation似乎并不明确. 可以从REPL中观察到此行为: scala> import scala.concurrent.Future, scala.concurrent.Exe
在未等待compless的情况下调用.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")
消除sun.misc.Unsafe的依赖是一个不幸的副作用.它在Scala 2.12和更新版本的Scala 2.11 IIRC中得到了纠正.
0

精彩评论

暂无评论...
验证码 换一张
取 消