Created
November 3, 2016 12:49
-
-
Save d6y/15731f22c489c298780ed9597660467c to your computer and use it in GitHub Desktop.
Alternative to Either[Error, Unit]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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