50 The IO type

 

After reading this lesson, you will be able to

  • Represent synchronous and asynchronous side effects lazily
  • Execute and process their result

Now that you’ve mastered the difference between eager and lazy evaluation, you are going to learn about cats.effect.IO. Impure functions produce side effects. The type IO, which is part of the cats-effect library, allows you to represent synchronous and asynchronous side effects lazily. You’ll be able to separate the definition of what to execute from its actual execution, making it easier to maintain and test. You can consider the type IO as the lazy alternative to the eagerly evaluated Future. After discussing why lazily evaluating side effects can be advantageous, you’ll see how to represent and execute side effects that are either synchronous or asynchronous. In the capstone, you’ll use the type IO to define side effects in your quiz application.

50.1 Why IO?

In the previous unit, you learned that the type Future can improve your application’s scalability and runtime performance by running expensive computations in the background. With time and experience, you will notice some of its stylistic disadvantages.

50.2 Project setup

50.3 Synchronous side effect

50.4 Asynchronous side effect

Summary

Answers to quick checks