IO
IO is a type class that represents an abstraction
for working with input/output (I/O) in functional programming.
It allows describing I/O operations without executing them immediately,
which helps avoid side effects and makes code more predictable and testable.
Where is it used?
- Asynchronous programming:
IOallows performing asynchronous operations, such as reading and writing files, network requests, and database interactions, without blocking the main execution thread. - Testing: Thanks to its abstraction,
IOmakes it easy to test code that performs I/O operations by replacing real operations with mocks or stubs. - Side effect management:
IOhelps control and manage side effects, allowing developers to explicitly specify when and how I/O operations should be executed. - Composing operations:
IOmakes it easy to combine various I/O operations, creating complex sequences of actions.
Advantages:
- Safety:
IOprevents unexpected side effects since operations are not executed immediately. - Readability: Code becomes more understandable and structured since I/O operations are explicitly described.
- Testability: It's easy to test functions that use
IOby replacing real operations with mocks or stubs.
Formal definition
IO is a type encapsulating computations that may interact with the outside world
(e.g., reading from a file, writing to a screen, networking, etc.).
Code definition in Scala
sealed trait IO[+A]:
private[effect] def apply(rw: World[RealWorld]): Trampoline[(World[RealWorld], A)]
Implementation in libraries
Implementation in ScalaZ
import scalaz.*
import Scalaz.*
import scalaz.effect.*
import scalaz.effect.IO.*
val action1 =
for
x <- readLn
_ <- putStrLn("Hello, " + x + "!")
yield ()
action1.unsafePerformIO()
Implementation in Cats
import cats.effect.IO
import cats.effect.unsafe.implicits.global
// Define an IO action that prints a string
val printHello: IO[Unit] = IO {
println("Hello, World!")
}
// Define an IO action that reads a string from the console
val readName: IO[String] = IO {
scala.io.StdIn.readLine("Enter your name: ")
}
// Define an IO action that composes the previous actions
val program: IO[Unit] =
for
_ <- printHello
name <- readName
_ <- IO { println(s"Hello, $name!") }
yield ()
// Run the program
program.unsafeRunSync()
References: