September 2018
Intermediate to advanced
398 pages
9h 43m
English
We mentioned earlier that our function, unsafeSave, has a side effect, which is to write to a file. But as functional programmers, we try to only write pure functions that have no side effects. However, at the end of the program, you still want this side effect to happen; otherwise, there would be no point in running it!
A common way of solving this dilemma is to use a parametrized type that encapsulates the side effect to run it asynchronously. A good candidate for that is the cats.effect.IO class in the cats.effect library (see https://typelevel.org/cats-effect/datatypes/io.html).
Here is an example that you can try in a Scala Console:
import cats.effect.IOval io = IO{ println("Side effect!"); 1 }// io: cats.effect.IO[Int] ...Read now
Unlock full access