Created
January 27, 2017 14:44
-
-
Save rparree/372bd8cd35e4962538c676d7d7f57488 to your computer and use it in GitHub Desktop.
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.{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