scala/scala-async

Support for cats-effect IO

XDracam opened this issue · 2 comments

A major problem with the Scala Future is that it is not referentially transparent.

Take for example, this block of code in the README:

def slowCalcFuture: Future[Int] = ...             
def combined: Future[Int] = async {               
  await(slowCalcFuture) + await(slowCalcFuture)   
}
val x: Int = Await.result(combined, 10.seconds)  

slowCalcFuture is awaited twice. This is fine as long as the Future isn't accidentally reused:

var i: Int = 0
val slowCalculation = Future { i += 1; i }
def slowCalcFuture = slowCalculation

This would make the future run only once, and the result value is simply used twice. combined == 2.

cats.effect.IO on the contrary is referentially transparent. You can freely reference the same IO and run it once or multiple times , however you please. Simply by using an IO, the above code results in combined == 3, because await(slowCalcFuture) would manually start slowCalcFuture.

I feel like this is how code should behave.

I haven't used this library yet, but I'd be interested in implementing IO support in a PR when there's some interest.

apparently @Baccata figured out how to make it work: typelevel/cats-effect#1924