Skip to content

Instantly share code, notes, and snippets.

@cb372
Last active April 11, 2016 11:59
Show Gist options
  • Save cb372/dd142d850c2962927d8b0436fe5773da to your computer and use it in GitHub Desktop.
Save cb372/dd142d850c2962927d8b0436fe5773da to your computer and use it in GitHub Desktop.
scala> def foo(a: Int): Future[Int] = memoize { Future { println("Computing..."); a + 1 } }
foo: (a: Int)scala.concurrent.Future[Int]
scala> foo(123)
res0: scala.concurrent.Future[Int] = List() // <-- WTF
scala> Computing...
scala> foo(123)
res1: scala.concurrent.Future[Int] = List() // <-- WTF
scala> res1
res2: scala.concurrent.Future[Int] = Success(124) // <-- oh ok never mind (?)
scala>
@cb372
Copy link
Author

cb372 commented Apr 11, 2016

I could also reproduce this more simply:

scala> Future { "wut" }
res11: scala.concurrent.Future[String] = List()

scala> res11
res12: scala.concurrent.Future[String] = Success(wut)

The problem went away with a fresh REPL.

@lloydmeta
Copy link

This is pretty interesting. I think I may have traced it down to the default implementation of Future, which is DefaultPromise that extends AbstractPromise.java.

When one of those is initialised, the very first thing that happens is the internal state gets set to Nil using updateState. I think this is a very small window, and I can't reproduce what you've shown, but I'm fairly certain that is the List() that you're seeing in res1 and res11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment