Skip to content

Instantly share code, notes, and snippets.

@rparree
Created January 27, 2017 14:44
Show Gist options
  • Save rparree/372bd8cd35e4962538c676d7d7f57488 to your computer and use it in GitHub Desktop.
Save rparree/372bd8cd35e4962538c676d7d7f57488 to your computer and use it in GitHub Desktop.
import scala.util.{Left, Right}
case class Name(value: String) extends AnyVal {
def +(s: Name) = Name(this.value + s)
override def toString: String = value
}
implicit def strToName(s: String) = Name(s)
def validateName(s: String): Either[String, Name] = {
if (s.isEmpty) Left("Name cannot be empty")
else Right(Name(s))
}
validateName("ss") match {
case Left(reason) => println(s"Oops: $reason")
case Right(name) => println(s"your name is ${name.value}")
}
for {
f <- validateName("Alliot")
l <- validateName("Alderson")
} yield f + " " + l
for {
f <- validateName("Alliot")
l <- validateName("")
} yield f + " " + l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment