Last active
May 27, 2016 11:56
-
-
Save milessabin/23ce8e31476f5c5a932a9c2b639e1ecb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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