Skip to content

Commit

Permalink
Providing a SyncConsole[F] instance, opt-in. Removing console from IO…
Browse files Browse the repository at this point in the history
…App.
  • Loading branch information
gvolpe committed Jun 1, 2018
1 parent 48cdb11 commit 04c44df
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
Expand Up @@ -18,7 +18,6 @@ package cats
package effect
package internals

import cats.Show
import cats.implicits._
import scala.concurrent.duration._

Expand Down Expand Up @@ -57,12 +56,4 @@ private[effect] object IOAppPlatform {
}

val defaultTimer: Timer[IO] = IOTimer.global

def defaultConsole: Console[IO] =
new Console[IO] {
def putStrLn[A: Show](a: A): IO[Unit] = IO(println(a.show))
def error[A: Show](a: A): IO[Unit] = IO(System.err.println(a.show))
def readLine: IO[String] = IO(scala.io.StdIn.readLine)
}

}
Expand Up @@ -18,7 +18,6 @@ package cats
package effect
package internals

import cats.Show
import cats.implicits._
import java.util.concurrent.CountDownLatch

Expand Down Expand Up @@ -64,12 +63,4 @@ private[effect] object IOAppPlatform {
}

def defaultTimer: Timer[IO] = IOTimerRef.defaultIOTimer

def defaultConsole: Console[IO] =
new Console[IO] {
def putStrLn[A: Show](a: A): IO[Unit] = IO(println(a.show))
def error[A: Show](a: A): IO[Unit] = IO(System.err.println(a.show))
def readLine: IO[String] = IO(scala.io.StdIn.readLine)
}

}
48 changes: 47 additions & 1 deletion core/shared/src/main/scala/cats/effect/Console.scala
Expand Up @@ -19,6 +19,7 @@ package effect

import cats.Show
import cats.instances.string._
import cats.syntax.show._

import scala.annotation.implicitNotFound

Expand Down Expand Up @@ -52,7 +53,7 @@ trait Console[F[_]] {
/**
* Reads line from the standard console.
*
* @return a [[String]] value representing the user's input or raise an error in the F context.
* @return a value representing the user's input or raise an error in the F context.
*/
def readLine: F[String]
}
Expand All @@ -78,3 +79,48 @@ object Console {
F.liftIO(console.readLine)
}
}

/**
* A default instance for any `F[_]` with a `Sync` instance.
*
* Use it either in a DSL style:
*
* {{{
* import cats.effect._
*
* object io extends SyncConsole[IO]
*
* import io._
*
* val program: IO[Unit] =
* for {
* _ <- putStrLn("Please enter your name: ")
* n <- readLine
* _ <- if (n.nonEmpty) putStrLn(s"Hello $n!")
* else error("Name is empty!")
* }
* }}}
*
* Or in tagless final style:
*
* {{{
* import cats.effect._
*
* def myProgram[F[_]](implicit C: Console[F]): F[Unit] =
* for {
* _ <- C.putStrLn("Please enter your name: ")
* n <- C.readLine
* _ <- if (n.nonEmpty) C.putStrLn(s"Hello $n!")
* else C.error("Name is empty!")
* }
* }}}
*
*/
class SyncConsole[F[_]](implicit F: Sync[F]) extends Console[F] {
def putStrLn[A: Show](a: A): F[Unit] =
F.delay(println(a.show))
def error[A: Show](a: A): F[Unit] =
F.delay(System.err.println(a))
def readLine: F[String] =
F.delay(scala.io.StdIn.readLine)
}
5 changes: 0 additions & 5 deletions core/shared/src/main/scala/cats/effect/IOApp.scala
Expand Up @@ -77,9 +77,4 @@ trait IOApp {
* On scala.js, the default is `Timer.global`.
*/
protected implicit def timer: Timer[IO] = IOAppPlatform.defaultTimer

/**
* Provides a default instance for Console[IO].
*/
def console: Console[IO] = IOAppPlatform.defaultConsole
}

0 comments on commit 04c44df

Please sign in to comment.