Type class in Scala

Type class is an important functional programming engineering idea. It’s designed to support Ad hoc polymorphism. It’s first introduced in Haskell. It’s easy to support it in Scala.

Let me give you a example implements type class Show.

First of all, define a Show trait.

trait Show[A]{
  def show(s: A): String

Then, for example, we want to log the class information which implements Show.

def log[A](a: A)(implicit s: Show[A]) = println(s.show(a))

Now, for each class we want to support the function, just define an implicit object extending Show.

case class Person(name: String)
implicit val personShow = new Show[Person]{
  def show(p: Person) = p.name

Done! You can just use log(p) to print p’s info.

However, for some types, provding a Show instance depends on some other types’ Show instances.

For instance, we could implement Show for Option:

case class Person(name: String)
implicit val optionShow[A](implicit sa: Show[A]) = new Show[Option[A]]{
  def show(oa: Option[A]) = a match{
    case None => "None"
    case Some(a) => "Some(" + sa.show(a) + ")"
 e }

Now, you can log Option classes.

scala> log(Option(Person("Ivan")))


Ivan Yang 30 March 2016
blog comments powered by Disqus