Skip to content

Instantly share code, notes, and snippets.

@d6y
Created November 3, 2016 12:49
Show Gist options
  • Save d6y/15731f22c489c298780ed9597660467c to your computer and use it in GitHub Desktop.
Save d6y/15731f22c489c298780ed9597660467c to your computer and use it in GitHub Desktop.
Alternative to Either[Error, Unit]
import scala.util.{Either, Right, Left}
object MarkerExample {
case class Error(msg: String)
sealed trait Success // or "NoValue" or "SuccessWithNoValue"?
object success extends Success {
override def toString: String = "success"
}
def write(): Either[Error, Success] = {
Right(success)
}
def log(): Either[Error, Success] = {
Left(Error("in log"))
//Right(success)
}
def run(): Either[Error, Success] =
for {
_ <- write()
_ <- log()
} yield success
def run0(): Either[Error, Success] = {
write().flatMap(_ => log())
// Hurrah! Wont' compile -> write().map(_ => log())
}
}
object Main {
def main(args: Array[String]): Unit = {
import MarkerExample._
println( run() )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment