Skip to content

Instantly share code, notes, and snippets.

@milessabin
Last active May 27, 2016 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milessabin/23ce8e31476f5c5a932a9c2b639e1ecb to your computer and use it in GitHub Desktop.
Save milessabin/23ce8e31476f5c5a932a9c2b639e1ecb to your computer and use it in GitHub Desktop.
import scala.language.higherKinds
object cats {
trait Show[A] {
def apply(x: A): String
}
object Show {
implicit def stringShow: Show[String] = new Show[String] {
def apply(x: String): String = x
}
}
}
trait Printer[A] {
def print(x: A): String
}
object Printer {
implicit def fallbackP[A]: Printer[A] =
new Printer[A] {
def print(x: A): String = x.toString
}
}
object CatsPrinter {
implicit def catP[A](implicit s: cats.Show[A]): Printer[A] =
new Printer[A] {
def print(x: A): String = s(x)
}
}
object DemoFallback {
def r[A](x: A)(implicit p: Printer[A]): String = p.print(x)
r("foo")
}
object DemoCats {
import CatsPrinter._
def r[A](x: A)(implicit p: Printer[A]): String = p.print(x)
r("foo")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment