Effect Systems

Let's explore effect systems in Scala. Imagine a pure functional program that looks something like the following:

val enProgram =
  for {
    _    <- write("What's your name? ")
    name <- readLn()
    _    <- write(s"Hello, ${name}!\n")
  } yield ()

val esProgram =
  for {
    _    <- write("¿Cómo te llamas? ")
    name <- readLn()
    _    <- write(s"¡Hola, ${name}!\n")
  } yield ()

val program =
  for {
    lang <- readEnv("LANG")
    _    <- if (lang.startsWith("es")) {
              esProgram
            } else {
              enProgram
            }
  } yield ()

This program looks up the $LANG environment variable, then performs some simple keyboard and console I/O in the user's regional language.

This program is itself just a value; it's a representation of a sequence of instructions. On its own, this program doesn't do anything and can't be run. Let's see how to run it using different effect systems: