Read Typeclass for Scala. Controversial? Yes. Problematic? Yes. Better than watching
your friends write string.toInt
and calling it good enough? Absolutely!
To use read in an existing SBT project with Scala 2.11 or a later version, add the following dependency to your
build.sbt
:
libraryDependencies += "io.chrisdavenport" %% "read" % "<version>"
import io.chrisdavenport.read.Read
import io.chrisdavenport.read.implicits._
"1".read[Int] // Right(1)
"Foo".read[Int] // Left
// For Those Who Want Haskell's Read
Read[Int].unsafeRead("1") // 1: Int