An example Scala ‘App’ for the Cats “IO Monad” article

As a quick note, if you’re interested in using the IO monad described in this IO Monad for Cats article, here’s the source code for a complete Scala App based on that article:

import cats.effect.IO

object Program extends App {

    val program = for {
        _    <- IO { println("Welcome to Scala!  What's your name?") }
        name <- IO { }
        _    <- IO { println(s"Well hello, $name!") }
    } yield ()


And here’s a build.sbt file you can use to run that App with SBT:

name := "CatsIOMonadExample"

version := "0.1"

scalaVersion := "2.12.2"

libraryDependencies ++= Seq(
    "org.typelevel" %% "cats" % "0.9.0",
    "org.typelevel" %% "cats-effect" % "0.3"

Running the App looks like this:

> run
[info] Compiling 1 Scala source ...
[info] Running Program 
Welcome to Scala!  What's your name?
Well hello, Fred!

See that article for more information. I just wanted to show a complete example, including the necessary SBT dependencies.

August, 2019 update: The Cats dependencies are now:

libraryDependencies ++= Seq(
   "org.typelevel" %% "cats-core" % "2.0.0-RC1",
   "org.typelevel" %% "cats-effect" % "1.3.1"

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.