This is a well-known problem with standard Future libraries: they are designed in such a way that they are not referentially transparent, as they readily evaluate and remember their result. In most cases, this is completely normal, and Scala developers rarely have to create the future without appreciation.
Run the following program:
val x = Future(...); f(x, x)
- this is not the same program as
f(Future(...), Future(...))
because in the first case, the future is estimated once, in the second - twice.
These are libraries that provide the necessary abstractions for working with object-transparent asynchronous tasks, the evaluation of which is postponed and not stored in memory, unless explicitly required by the developer.
- Scalaz challenge
- Monix Challenge
- fs2
If you want to use Cats, the effects of Cats work great with both Monix and fs2.
source share