The title is right, I want everything.
- In eager evaluation the functions are evaluated always as soon as the assignment is done.
- In lazy evaluation this is not done until the value is needed, but then it is a blocking operation, just like in eager evaluation, but delayed.
- I would like to have something that starts the evaluation eagerly and concurrently (not blocking) until the value is needed.
I know this is a bit strange, so I made an illustrative gist.
It works perfectly in this example and the computation of the lazy eager value seems to be for free, while the other two values take 10 seconds each. Sure, things are happening in another thread and so on, but I'm fine with that, I would simply like to have some option to do things like this, sometimes, when needed.
I've been thinking in more elegant ways to define this, functions, annotations, etc. In the end the only possibility I found are macros, which are not available in Scala (AFAIK).
Is there any way to do this in an elegant way or what I wrote is as elegant as it gets?
PD: just to make it more clear. This is what it takes to do what I want to do:
val eager_lazy_aux = Future(longComputation(1))
lazy val eager_lazy = Await.result(eager_lazy_aux, Duration.Inf)
This is what I would like to do:
parallel val eager_lazy = longComputation(1)
I'm not a big fan of implicits, but maybe adding this is the best that we can get.
implicit def getFuture[T](f: Future[T]): T = Await.result(f, Duration.Inf)