Created
July 28, 2014 17:55
-
-
Save tstone/027abafe1a7bc9466f9c to your computer and use it in GitHub Desktop.
A little shortcut around always having to specify Left or Right
This file contains hidden or 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
implicit def aToEither[A,B](a: A): Either[A, B] = Left(a) | |
implicit def bToEither[A,B](b: B): Either[A, B] = Right(b) | |
def f(input: Either[String, Int]) = input match { | |
case Left(s) => println(s"Hello, $s") | |
case Right(i) => println(s"You are $i years old") | |
} | |
scala> f(10) | |
You are 10 years old |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment