Skip to content

Instantly share code, notes, and snippets.

@nrinaudo
Created January 24, 2018 22:42
Show Gist options
  • Save nrinaudo/7171954254d3743c004762f03e54813f to your computer and use it in GitHub Desktop.
Save nrinaudo/7171954254d3743c004762f03e54813f to your computer and use it in GitHub Desktop.
Semi-automatic derivation
import io.circe._
import io.circe.generic.semiauto._
import org.scalacheck._, derive._
sealed trait Or[+A, +B] extends Product with Serializable
final case class Left[A](a: A) extends Or[A, Nothing]
final case class Right[B](b: B) extends Or[Nothing, B]
object Or {
// Compiles
implicit def decoder[A: Decoder, B: Decoder]: Decoder[A Or B] = deriveDecoder
// Does not compile
implicit def cogen[A: Cogen, B: Cogen]: Cogen[A Or B] = MkCogen[A Or B].cogen
}
@nrinaudo
Copy link
Author

What I can't really work out yet is how circe manages this behaviour:

implicitly[Decoder[Int Or Boolean]] // Fine

case class Foobar(foo: Int, bar: Boolean)

implicitly[Decoder[Int Or Foobar]] // Missing implicit

How does it know to derive instances for Left[Int] and Right[Boolean], but not Foobar ?!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment