Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `Future#toString` returns `"List()"`?

Tags:

scala

future

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.

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()

Note that Scala 2.12.x will likely explicitly implement Future#toString to return "Future(<not completed>)" instead (source).


Edit: 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")
like image 260
OlivierBlanvillain Avatar asked Jul 20 '16 10:07

OlivierBlanvillain


People also ask

Why do we think about the future?

Thus, research suggests that thinking about the future can motivate us to take the steps necessary to reach our goals—but only if we take obstacles into account. 3. Improves psychological well-being Besides helping us make decisions and reach our goals, there is evidence that prospection may improve psychological health more generally.

Can we predict our own futures?

We can make predictions about our own futures based on what we’ve learned about other people’s experiences and even from characters in books and movies. And we can consider multiple directions our futures might take. It is this remarkable ability to simulate our possible futures that makes prospection special.

What does future stand for?

Nayvadius DeMun Wilburn (born November 20, 1983), known professionally as Future, is an American rapper, singer, songwriter, and record producer. Born and raised in Atlanta, Georgia, Wilburn first became involved in music as part of the Dungeon Family collective, where he was nicknamed the Future.

What is the future of human beings?

The future of human beings is not very bright , we consider us to be the centre of the whole creation and the most intelligent species on the planet. Now what would you call a person who is conteniously trying to cut the branch on which he is sitting ? Intelligent? I don’t think so, this is what the human race is doing by ruining the planet .


1 Answers

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.

like image 186
Viktor Klang Avatar answered Oct 09 '22 19:10

Viktor Klang