Is this possible in scala def convert(f: () ⇒ Future[Int]): Future[() ⇒ Int] =?

  • A+
Category:Languages
def computeComplexNumber: () ⇒ Future[Int] =   () ⇒     Future {       println("Computing complex number ...")       Int.MaxValue   }  def convert(f: () ⇒ Future[Int]): Future[() ⇒ Int] = ???  convert(computeComplexNumber) 

Is it possible to implement convert method with above signature with condition that when convert(computeComplexNumber) is called, nothing gets printed unless you do this

Await.result(convert(computeComplexNumber), 5.seconds)() 

 


I don't see any way to do it without blocking inside convert. There is simply no way to get hold of an instance of Future[X] inside of convert, without invoking f. But once you invoke f, the Future { ... } will eagerly start to evaluate the block with the println, and produce an output.

You probably want to take a look at scalaz.concurrent.Task instead.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: