I'm trying to implement the Absurd
typeclass (as seen in Haskell's Data.Boring library) in Scala.
I'm able to define an Absurd
instance for Nothing
.
Unfortunately, when I try to define an absurd instance for Either
, I get a missing implicit error
sealed trait Absurd[A] {
def absurd[X](a: A): X
}
object Absurd {
def apply[A: Absurd, B](a: A):B = implicitly[Absurd[A]].absurd[B](a)
implicit val absurdForNothing: Absurd[Nothing] = new Absurd[Nothing]{
override def absurd[X](a: Nothing): X = a
}
implicit def absurdForEither[A: Absurd, B: Absurd]: Absurd[Either[A, B]] = new Absurd[Either[A, B]]{
override def absurd[X](a: Either[A,B]): X = a match {
case Left(a) => Absurd[A, X](a)
case Right(b) => Absurd[B, X](b)
}
}
}
This compiles:
implicitly[Absurd[Nothing]]
This fails to compile:
implicitly[Absurd[Either[Nothing, Nothing]]]
I'm using Scala Version "2.13.2".
It is possibly intersting to note, that the following, very similar code (which doesn't involve Nothing
), does compile:
trait SomeTypeclass[A]
case class SomeInstance()
object SomeTypeclass {
implicit val someTypeclassForSomeInstance: SomeTypeclass[SomeInstance] = new SomeTypeclass[SomeInstance] {}
implicit def someTypeclassForEither[A: SomeTypeclass, B: SomeTypeclass]: SomeTypeclass[Either[A, B]] = new SomeTypeclass[Either[A, B]] {}
}
object SomeApplicationCode {
implicitly[SomeTypeclass[Either[SomeInstance, SomeInstance]]]
}