I have just started to learn scalaz from eugene's blog post. I am trying to use === in my code but it returns a compile error
value === is not a member of object Red
Here's is my code
import scalaz._
sealed trait TrafficLight
case object Red extends TrafficLight
case object Yellow extends TrafficLight
case object Green extends TrafficLight
object Equality {
import Scalaz._
def eqInstance[A](f: (A,A) => Boolean): Equal[A] = new Equal[A]{
def equal(a1: A, a2: A): Boolean = f(a1,a2)
}
def trafficLightEqualFunction(a1: TrafficLight, a2: TrafficLight): Boolean = (a1, a2) match {
case (Red, Red) => true
case (Yellow, Yellow) => true
case (Green, Green) => true
case _ => false
}
implicit val trafficLightEqual: Equal[TrafficLight] = eqInstance(trafficLightEqualFunction)
def main(args: Array[String]){
println(Red === Red)
}
}
That's exactly what his blog complains. The type of Red
is Red.type
and there is no instance of scalaz.Equal
for this type because Equal is invariant, so despite:
Red <: TrafficLight
it doesn't follow
Equal[Red] <: Equal[TrafficLight]
.
You can amend that by making Scala see Red as TrafficLight:
println((Red:TrafficLight) === (Red:TrafficLight))