JIT: What’s an IO? (Scala 3 Video)
At this point I could turn you over to the ZIO and Cats Effect websites and their documentation, and you might do pretty well. But instead of doing that just yet, I’d like to help you out a little more.
The first thing to say is that before you’re 100% ready for a ZIO
, it will help to look at an IO
.
A bit of FP history
As a wee bit of FP history, back in the early days of Haskell, some really smart people were trying to figure out how to handle I/O in a “pure” way. Every other function in Haskell was pure ... and then there was I/O.
Long story short, they invented a concept called an IO
type. To start with, you can think of IO
as being like Try
or Either
: it’s a type you want to use when you communicate with the outside world, or run any other side effect. So wherever I have function signatures like this in this book:
def readFile(f: String): Try[String]
def writeFile(s: String): Try[Unit]
conceptually, you can replace those Try
types with the IO
type:
def readFile(f: String): IO[String]
def writeFile(s: String): IO[Unit]
This is a slight oversimplification, but not too much. For example, in Functional Programming, Simplified, I show how to write an IO
class and use it in for
expressions, just like we’ve used Try
in this book.
Three keys about IO types
Fast-forward to today, and there are three important things to know about IO
types:
- They were created to let you reason about your I/O and side-effect code using algebraic substitution.
- One thing I haven’t mentioned yet is that you can’t mix
Option
,Try
, andEither
values insidefor
expressions, but, you can use as manyIO
s as you need. - To help with Item 2,
IO
s are likeTry
andEither
, but on steroids. They are much more powerful than those types, with tons of built-in features. (Types likeTry
andEither
can be written in less than a page of Scala code, butIO
types have been developed over years of effort.)
So let’s talk about IO
types a little bit.
Update: All of my new videos are now on
LearnScala.dev