left and right implicit conversion to Either in Scalaz
One way to convert any value to a scalaz Either or \/ is via the left and right implicit conversions:
import scalaz._
import Scalaz._
5.right[String]
String \/ Int = \/-(5)
"This is an error".left[Int]
String \/ Int = -\/(This is an error)
The basic format is:
.right[left_type]
right_instance.left[right_type] left_instance
Importing Scalaz._ brings a lot of unnecessary implicit conversions and types into scope. What if you wanted something a little more light weight?
scalaz 7.0.x
After hunting around I found that the left and right methods are defined in the scalaz.syntax.IdOps trait and are implemented in the scalaz.syntax.id object. With that information we can now use cheaper imports for left and right:
import scalaz._
import syntax.id._
5.right[String]
String \/ Int = \/-(5)
"This is an error".left[Int]
String \/ Int = -\/(This is an error)
scalaz 7.1.x onwards
In scalaz 7.1.x and onwards, the left and right methods are defined in the scalaz.syntax.EitherOps trait and implemented in the scalaz.syntax.either object. Your imports would have to change to:
import scalaz._
import syntax.either._
5.right[String]
String \/ Int = \/-(5)
"This is an error".left[Int]
String \/ Int = -\/(This is an error)