Skip to content

Instantly share code, notes, and snippets.

@hamishdickson
Created August 7, 2017 19:12
Show Gist options
  • Save hamishdickson/a66d4933dd3938284b1751cf5af646cb to your computer and use it in GitHub Desktop.
Save hamishdickson/a66d4933dd3938284b1751cf5af646cb to your computer and use it in GitHub Desktop.
import cats._
import cats.data._
import cats.implicits._
import shapeless._
object GeneralShow {
implicit val hnilShow = Show.show[HNil](_ => "")
implicit def hconsShow[H, T <: HList](
implicit
hShow: Show[H],
tShow: Show[T]
): Show[H :: T] = new Show[H :: T] {
def show(ls: H :: T): String = ls.head.show + " " + ls.tail.show
}
implicit def autoShow[T, Repr](
implicit
genericT: Generic.Aux[T, Repr],
genericShow: Lazy[Show[Repr]]
): Show[T] = new Show[T] {
def show(t: T): String = genericShow.value.show(genericT.to(t))
}
}
object Test {
import GeneralShow._
implicitly[Show[Int :: String :: HNil]]
case class Foo(i: Int, s: String)
case class Bar(f: Foo)
implicitly[Show[Bar]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment